Give a very good method to count the number of ones in a 32
bit number.
(caution: looping through testing each bit is not a solution)

Answers were Sorted based on User's Feedback



Give a very good method to count the number of ones in a 32 bit number. (caution: looping through ..

Answer / raghuram

#include<iostream.h>
#include<conio.h>
/*no. of 1's in no. of 1's steps*/
int count(unsigned long int n)
{
int count=0;
while(n)
{
count++;
n=n&n-1;
}
return count ;
}

Is This Answer Correct ?    41 Yes 26 No

Give a very good method to count the number of ones in a 32 bit number. (caution: looping through ..

Answer / vijay

#include<stdio.h>
main()
{
int x=123,i=0;
while(x)
{
x=x&(x-1);
i++;
}
printf("Number of set bits are %d \n",i);
}

Is This Answer Correct ?    24 Yes 9 No

Give a very good method to count the number of ones in a 32 bit number. (caution: looping through ..

Answer / xyz

main()
{
int i=1177;
int j=0;
while(i>0)
{
if((i%2)!=0)
j++;
i=i/2;
}
printf("The number of one is %d\n", j);
}

Is This Answer Correct ?    14 Yes 6 No

Give a very good method to count the number of ones in a 32 bit number. (caution: looping through ..

Answer / lawrence

@Turk
your solution is O(n) not O(logn). your algorithm still
counts each zeros and ones.

Is This Answer Correct ?    2 Yes 1 No

Give a very good method to count the number of ones in a 32 bit number. (caution: looping through ..

Answer / sunny arora

/* This is a table which will contain number of ones
corrosponding to number value */
static int bits_in_char [256] ;

int bitcount (unsigned int n)
{
// works only for 32-bit ints

return bits_in_char [n & 0xffu]
+ bits_in_char [(n >> 8) & 0xffu]
+ bits_in_char [(n >> 16) & 0xffu]
+ bits_in_char [(n >> 24) & 0xffu] ;
}

Is This Answer Correct ?    9 Yes 9 No

Give a very good method to count the number of ones in a 32 bit number. (caution: looping through ..

Answer / turk

Here is an O(logn) solution

#include<stdio.h>

int numberOfOnesByte(unsigned char c, int length){
if (c==0)
return 0;
if (c==1)
return 1;
unsigned char left, right;
int lengthP = length/2;
left = c>>lengthP;
right = c-(left<<lengthP);
return numberOfOnesByte(left,length-lengthP)+numberOfOnesByte(right,lengthP);
}
int numberOfOnes(unsigned char *array, int start, int end, int length){
if(length==1){
return numberOfOnesByte(array[start],8);
} else {
int lengthP = length/2;
return numberOfOnes(array,start,start+lengthP-1,lengthP)+
numberOfOnes(array,start+lengthP,end,length-lengthP);
}
}
int main(){
unsigned char array[8] = {0xFF,0XAA,0xFF,0XAA,0xFF,0XAA,0xFF,0XAA};
printf("number of bits %d\n",numberOfOnes(array,0,7,8));
return 0;
}

Is This Answer Correct ?    3 Yes 5 No

Give a very good method to count the number of ones in a 32 bit number. (caution: looping through ..

Answer / pgmrsf

static void NumOfOnes(uint n)
{
int c = 0;
for (int i=0; i<10; i++)
{
if ((n & (int)Math.Pow(2, i)) == (int)Math.Pow(2,
i))
{
c++;
}
}
Console.WriteLine("{0}", c);
}

Is This Answer Correct ?    4 Yes 7 No

Post New Answer

More C Code Interview Questions

#include<stdio.h> main() { const int i=4; float j; j = ++i; printf("%d %f", i,++j); }

1 Answers  


main( ) { static int a[ ] = {0,1,2,3,4}; int *p[ ] = {a,a+1,a+2,a+3,a+4}; int **ptr = p; ptr++; printf(“\n %d %d %d”, ptr-p, *ptr-a, **ptr); *ptr++; printf(“\n %d %d %d”, ptr-p, *ptr-a, **ptr); *++ptr; printf(“\n %d %d %d”, ptr-p, *ptr-a, **ptr); ++*ptr; printf(“\n %d %d %d”, ptr-p, *ptr-a, **ptr); }

2 Answers   Persistent,


main() { int x=5; for(;x!=0;x--) { printf("x=%d\n", x--); } } a. 5, 4, 3, 2,1 b. 4, 3, 2, 1, 0 c. 5, 3, 1 d. none of the above

2 Answers   HCL,


#include <stdio.h> int main(void) { int a=4, b=2; a=b<<a+b>>2 ; printf("%d",a); return 0; }

0 Answers   Student,


main() { int i; printf("%d",scanf("%d",&i)); // value 10 is given as input here }

2 Answers   IBM,






Finding a number which was log of base 2

1 Answers   NetApp,


int DIM(int array[]) { return sizeof(array)/sizeof(int ); } main() { int arr[10]; printf(“The dimension of the array is %d”, DIM(arr)); }

2 Answers   CSC,


why do you use macros? Explain a situation where you had to incorporate macros in your proc report? use a simple instream data example with code ?

0 Answers  


void main() { while(1){ if(printf("%d",printf("%d"))) break; else continue; } }

1 Answers  


What is full form of PEPSI

0 Answers  


Ramesh’s basic salary is input through the keyboard. His dearness allowance is 40% of basic salary, and house rent allowance is 20% of basic salary. Write a program to calculate his gross salary.

1 Answers  


char *someFun() { char *temp = “string constant"; return temp; } int main() { puts(someFun()); }

1 Answers  


Categories