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
Explain the bubble sort algorithm.
write a program to print largest number of each row of a 2D array
What is %s and %d in c?
How can you be sure that a program follows the ANSI C standard?
Explain the use of keyword 'register' with respect to variables.
What is line in c preprocessor?
What are 'near' and 'far' pointers?
How we can insert comments in a c program?
What are the different types of pointers used in c language?
What are the 4 types of programming language?
What is assert and when would I use it?
Can we initialize extern variable in c?
a number whose only prime factors are 2,3,5, and 7 is call humble number,,write a program to find and display the nth element in this sequence.. sample input : 2,3,4,11,12,13, and 100.. sample output : the 2nd humble number is 2,the 3rd humble number is 3,the 4th humble number is ,the 11th humble number is 12, the 12th humble number is 14, the 13th humble number is 15, the 100th humble number is 450.
What is c definition?
What is meant by keywords in c?