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
Can we change the value of #define in c?
What are the main characteristics of c language describe the structure of ac program?
What is a null pointer in c?
Implement bit Array in C.
How can I ensure that integer arithmetic doesnt overflow?
What does printf does?
how many types of operators are include in c language a) 4 b) 6 c) 8 d) 12
What is the difference between text and binary i/o?
How do you determine the length of a string value that was stored in a variable?
Can we assign integer value to char in c?
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
Differentiate Source Codes from Object Codes
Why is extern used in c?
Are bit fields portable?
how do you write a function that takes a variable number of arguments? What is the prototype of printf () function?