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                      
tip   SiteMap shows list of All Categories in this site.
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 ?    0 Yes 1 No
Xyz
 

 
 
 
Other C Code Interview Questions
 
  Question Asked @ Answers
 
main() { int i=_l_abc(10); printf("%d\n",--i); } int _l_abc(int i) { return(i++); }  1
main() { char * strA; char * strB = I am OK; memcpy( strA, strB, 6); } a. Runtime error. b. I am OK c. Compile error d. I am O HCL2
main() { char *str1="abcd"; char str2[]="abcd"; printf("%d %d %d",sizeof(str1),sizeof(str2),sizeof("abcd")); }  1
main() { printf("%d", out); } int out=100;  1
plz send me all data structure related programs  2
main() { static char names[5][20]={"pascal","ada","cobol","fortran","perl"}; int i; char *t; t=names[3]; names[3]=names[4]; names[4]=t; for (i=0;i<=4;i++) printf("%s",names[i]); }  1
int DIM(int array[]) { return sizeof(array)/sizeof(int ); } main() { int arr[10]; printf(“The dimension of the array is %d”, DIM(arr)); }  1
#define DIM( array, type) sizeof(array)/sizeof(type) main() { int arr[10]; printf(“The dimension of the array is %d”, DIM(arr, int)); }  1
Write out a function that prints out all the permutations of a string. For example, abc would give you abc, acb, bac, bca, cab, cba. You can assume that all the characters will be unique. Microsoft4
enum colors {BLACK,BLUE,GREEN} main() { printf("%d..%d..%d",BLACK,BLUE,GREEN); return(1); }  1
main() { char *p="GOOD"; char a[ ]="GOOD"; printf("\n sizeof(p) = %d, sizeof(*p) = %d, strlen(p) = %d", sizeof(p), sizeof(*p), strlen(p)); printf("\n sizeof(a) = %d, strlen(a) = %d", sizeof(a), strlen(a)); }  1
#include<stdio.h> main() { const int i=4; float j; j = ++i; printf("%d %f", i,++j); }  1
Write a routine that prints out a 2-D array in spiral order Microsoft2
program to Reverse a linked list Ness-Technologies4
How to reverse a String without using C functions ? Wipro13
main() { static int a[3][3]={1,2,3,4,5,6,7,8,9}; int i,j; static *p[]={a,a+1,a+2}; for(i=0;i<3;i++) { for(j=0;j<3;j++) printf("%d\t%d\t%d\t%d\n",*(*(p+i)+j), *(*(j+p)+i),*(*(i+p)+j),*(*(p+j)+i)); } }  1
Given an array of characters which form a sentence of words, give an efficient algorithm to reverse the order of the words (not characters) in it. Wipro2
main() { int i=0; for(;i++;printf("%d",i)) ; printf("%d",i); }  1
Give a oneline C expression to test whether a number is a power of 2? Motorola16
main() { int k=1; printf("%d==1 is ""%s",k,k==1?"TRUE":"FALSE"); }  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