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

Answers were Sorted based on User's Feedback



how do u find out the number of 1's in the binary representation of a decimal number without ..

Answer / ravi saini

unsigned int count=0,no;
/*
Enter no Here................
*/
while(no ! =0)
{
if((no & 0x01) ==1)
count++;
no=no >>1
}
printf("%d",count);

Is This Answer Correct ?    9 Yes 3 No

how do u find out the number of 1's in the binary representation of a decimal number without ..

Answer / barun

int x =0x1;
static count;
while(no ! =0)
{
if((no >>1 & x) ==1)
count+=1;
}
printf("%d",count);

Is This Answer Correct ?    7 Yes 2 No

how do u find out the number of 1's in the binary representation of a decimal number without ..

Answer / srsabariselvan

int main()
{
int n,i=0;
scanf("%d",&n);
while(n!=0)
{
if(n&01)
i++;
n>>=1;
}
printf("%d",i);
}

Is This Answer Correct ?    3 Yes 2 No

how do u find out the number of 1's in the binary representation of a decimal number without ..

Answer / krishna

The above solutions will not work for negative numbers.
if a negative number is right shifted, the most significant bit will become 1. so the number never becomes zero.

The following will work for both +ve and -ve numbers.

#include <stdio.h>
int main(int argc, char *argv[]) {
int count = 0;
int no = 3;
int x = 0x01;

if ( argc > 1) {
no = atoi(argv[1]);
}

while ( x != 0) {
if( no & x ) {
count++;
}
x <<= 1;
}

printf( "number: %d , no.of 1s in it: %d\n", no, count);
return 0;
}

Is This Answer Correct ?    1 Yes 2 No

how do u find out the number of 1's in the binary representation of a decimal number without ..

Answer / 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

More C Interview Questions

exit () is used to a) exit () terminates the execution of the program itself b) exit () terminates the execution of the loop c) exit () terminates the execution of the block d) none of the above

0 Answers  


Explain what does the characters 'r' and 'w' mean when writing programs that will make use of files?

0 Answers  


Write a program that his output 1 12 123

0 Answers  


What does int main () mean?

0 Answers  


Hai sir, I had planned to write the NIC scientific engineer exam , plz post the sample question......

0 Answers  






What is the difference between strcpy() and memcpy() function in c programming?

0 Answers  


What are valid operations on pointers?

0 Answers  


What does the format %10.2 mean when included in a printf statement?

0 Answers  


what will be the result of the following program ? char *gxxx() { static char xxx[1024]; return xxx; } main() { char *g="string"; strcpy(gxxx(),g); g = gxxx(); strcpy(g,"oldstring"); printf("The string is : %s",gxxx()); } a) The string is : string b) The string is :Oldstring c) Run time error/Core dump d) Syntax error during compilation e) None of these

2 Answers   IBM,


What is 'makefile' in C langauage? How it be useful? How to write a makefile to a particular program?

2 Answers  


What are the advantages of using Unions?

0 Answers   IBS,


What are control structures? What are the different types?

0 Answers  


Categories