Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

how do u find out the number of 1's in the binary
representation of a decimal number without converting it
into binary(i mean without dividing by 2 and finding out
the remainder)? three lines of c code s there it
seems...can anyone help

Answer Posted / jay

Well bitwise operators are one way to do it. But for
positive integers, I thought up another solution. I think
this could work for int n:

for(int i = 0, int count = 0; i < sizeOf(n) / 8.0; i++){
count += (n - 2^i > 0) ? 1 : 0;
}

Is that what you meant by 3 lines?

Is This Answer Correct ?    0 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Can we change the value of #define in c?

973


What are the main characteristics of c language describe the structure of ac program?

1168


What is a null pointer in c?

1149


Implement bit Array in C.

1108


How can I ensure that integer arithmetic doesnt overflow?

1110


What does printf does?

1204


how many types of operators are include in c language a) 4 b) 6 c) 8 d) 12

1035


What is the difference between text and binary i/o?

1018


How do you determine the length of a string value that was stored in a variable?

1097


Can we assign integer value to char in c?

1180


Write a C program to accept a matrix of any size. Find the frequency count of each element in the matrix and positions in which they appear in the matrix

1968


Differentiate Source Codes from Object Codes

1496


Why is extern used in c?

1063


Are bit fields portable?

1116


how do you write a function that takes a variable number of arguments? What is the prototype of printf () function?

1908