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

Explain the red-black trees?

611


What are header files why are they important?

586


How to explain the final year project as a fresher please answer with sample project

472


Is c language still used?

540


What type is sizeof?

587






explain what are actual arguments?

639


What is the difference between int main and void main?

578


Calculate 1*2*3*____*n using recursive function??

1522


How can I remove the leading spaces from a string?

638


Is it fine to write void main () or main () in c?

549


Why clrscr is used in c?

589


What is the scope of static variable in c?

537


How can you invoke another program from within a C program?

620


List at least 10 sorting methods indicating their average case complexity, worst case complexity and best case complexity.

2305


How can a program be made to print the line number where an error occurs?

654