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
What are the different types of linkage exist in c?
What is the purpose of scanf() and printf() functions?
Is c is a procedural language?
When is a “switch” statement preferable over an “if” statement?
What is wrong with this declaration?
Write a C++ program to generate 10 integer numbers between - 1000 and 1000, then store the summation of the odd positive numbers in variable call it sum_pos, then find the maximum digit in this variable regardless of its digits length.
What is the use of header?
What are runtime error?
How can you pass an array to a function by value?
What are the applications of c language?
Should I learn c before c++?
FILE PROGRAMMING
Which is the memory area not included in C program? give the reason
Do variables need to be initialized?
What is #include called?