ALLInterview.com :: Home Page KalAajKal.com
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage  Contact Us     Login  |  Sign Up                      
Do you have a collection of Interview Questions and interested to share with us!!
Please send that collection to along with your userid / name. ThanQ
Google
 
Categories  >>  Code Snippets  >>  Programming Code  >>  C Code
 
 


 

 
 C Code interview questions  C Code Interview Questions
 C++ Code interview questions  C++ Code Interview Questions
 VC++ Code interview questions  VC++ Code Interview Questions
 Java Code interview questions  Java Code Interview Questions
 Dot Net Code interview questions  Dot Net Code Interview Questions
 Visual Basic Code interview questions  Visual Basic Code Interview Questions
 Programming Code AllOther interview questions  Programming Code AllOther Interview Questions
Question
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)
 Question Submitted By :: =-PKG-=
I also faced this Question!!     Rank Answer Posted By  
 
  Re: 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)
Answer
# 1
#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 ?    14 Yes 9 No
Raghuram
 
  Re: 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)
Answer
# 2
/* 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 ?    3 Yes 4 No
Sunny Arora
 
 
 
  Re: 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)
Answer
# 3
#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 ?    7 Yes 2 No
Vijay
 
  Re: 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)
Answer
# 4
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 ?    1 Yes 2 No
Pgmrsf
 
  Re: 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)
Answer
# 5
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 ?    1 Yes 1 No
Xyz
 

 
 
 
Other C Code Interview Questions
 
  Question Asked @ Answers
 
main() { unsigned int i=10; while(i-->=0) printf("%u ",i); }  1
how to check whether a linked list is circular.  3
main() { int c = 5; printf("%d", main||c); } a. 1 b. 5 c. 0 d. none of the above HCL1
char *someFun1() { char temp[ ] = “string"; return temp; } char *someFun2() { char temp[ ] = {‘s’, ‘t’,’r’,’i’,’n’,’g’}; return temp; } int main() { puts(someFun1()); puts(someFun2()); }  1
main() { show(); } void show() { printf("I'm the greatest"); }  1
main() { int a[10]; printf("%d",*a+1-*a+3); }  1
main() { char *a = "Hello "; char *b = "World"; clrscr(); printf("%s", strcpy(a,b)); } a. “Hello” b. “Hello World” c. “HelloWorld” d. None of the above HCL1
main() { static int var = 5; printf("%d ",var--); if(var) main(); }  1
main() { char *p; p="Hello"; printf("%c\n",*&*p); }  1
There were 10 records stored in “somefile.dat” but the following program printed 11 names. What went wrong? void main() { struct student { char name[30], rollno[6]; }stud; FILE *fp = fopen(“somefile.dat”,”r”); while(!feof(fp)) { fread(&stud, sizeof(stud), 1 , fp); puts(stud.name); } }  1
#define clrscr() 100 main() { clrscr(); printf("%d\n",clrscr()); }  1
How we print the table of 3 using for loop in c programing?  3
main() { int i=5,j=6,z; printf("%d",i+++j); }  1
In the following pgm add a stmt in the function fun such that the address of 'a' gets stored in 'j'. main(){ int * j; void fun(int **); fun(&j); } void fun(int **k) { int a =0; /* add a stmt here*/ }  1
#include<stdio.h> main() { struct xx { int x; struct yy { char s; struct xx *p; }; struct yy *q; }; }  1
Write a procedure to implement highlight as a blinking operation  1
Sorting entire link list using selection sort and insertion sort and calculating their time complexity NetApp1
void main() { while(1){ if(printf("%d",printf("%d"))) break; else continue; } }  1
#define SQR(x) x * x main() { printf("%d", 225/SQR(15)); } a. 1 b. 225 c. 15 d. none of the above HCL1
int i=10; main() { extern int i; { int i=20; { const volatile unsigned i=30; printf("%d",i); } printf("%d",i); } printf("%d",i); }  1
 
For more C Code Interview Questions Click Here 
 
 
 
 
 
   
Copyright Policy  |  Terms of Service  |  Help  |  Site Map 1  |  Articles  |  Site Map  |   Site Map  |  Contact Us interview questions urls   External Links 
   
Copyright © 2007  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  Forum9.com   ::  KalAajKal.com