GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Tech FAQs  >  OOPS
Go To First  |  Previous Question  |  Next Question 
 OOPS  |  Question 4 of 258    Print  
What is the most efficient way to count the number of bits which are set in a value?

  
Total Answers and Comments: 7 Last Update: May 30, 2008   
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: ptusha
 

int fnCountbits(int num )
{
 int iCount = 0;
 while ( num )
 {
   num &= (num-1) ;
  iCount++;
 } 

return iCount;

}



Above answer was rated as good by the following members:
Sudheendra.S.N
July 20, 2005 12:20:29   #1  
Santosh        

RE: What is the most efficient way to count the number of bits which are set in a value?
shifting each bit left till the no of bits end
 
Is this answer useful? Yes | No
September 06, 2005 04:55:36   #2  
Paresh        

RE: What is the most efficient way to count the number of bits which are set in a value?
foo(unsigned char *ByteField)
{
int i 0 j 0;
unsigned char *BytePos;
short Bit;

BytePos ByteField + sizeof(ByteField);

for (i 0; i {
if (*(BytePos - i) > (char ) 0)
{
for (j 0; j<8; j++)
{

if (((*(BytePos - i)) & 1< {
Bit (i * 8) + j;
printf( Bit Set d n Bit);
}
}
}
}
}

Should give u the bit which are set

 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 1Overall Rating: -1    
October 21, 2005 07:10:41   #3  
Raks        

RE: What is the most efficient way to count the numbe...

#define ISBIT(pos val) ((1<<(pos-1))&val)


void main(void)
{
int i 0x00;
int cnt 0;
int pos 0;
for(pos 1;pos< (sizeof(i)*8);pos++)
{
if(ISBIT(pos i))
{
cnt++;
}
}

printf( Count d cnt);
}


 
Is this answer useful? Yes | No
January 27, 2006 23:57:26   #4  
prakash        

RE: What is the most efficient way to count the numbe...

int cnt_bits(int num) {

int cnt 0;

while(num) {

if(num 2) cnt++;

num / 2;

}

return cnt;

}


 
Is this answer useful? Yes | No
August 17, 2006 23:58:22   #5  
ptusha        

RE: What is the most efficient way to count the numbe...

int fnCountbits(int num )
{
int iCount 0;
while ( num )
{
num & (num-1) ;
iCount++;
}

return iCount;

}


 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
August 23, 2006 15:34:43   #6  
Visitor        

RE: What is the most efficient way to count the numbe...
Real smart solution to this question is to use a LookUp table.
 
Is this answer useful? Yes | No
May 30, 2008 15:13:46   #7  
gunturi.prabhakar Member Since: May 2008   Contribution: 4    

RE: What is the most efficient way to count the number of bits which are set in a value?
is this auestion refers to how many bits are set to 1 in a value.
if that is the question then simply convert that value into bit code....
then you can get the answer easily.....

 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape