GeekInterview.com
   Home |  Tech FAQ  |   Interview Questions |  Placement Papers |  Tech Articles |  Learn |  Freelance Projects |  Online Testing |  Geeks Talk |  Job Postings |  Knowledge Base | Site Search |  Add/Ask Question

  GeekInterview.com  >  Tech FAQs  >  OOPS

 Print  |  
Question:   What is the most efficient way to count the number of bits which are set in a value?



October 10, 2005 07:10:41 #3
 Raks   Member Since: Visitor    Total Comments: N/A 

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);
}

     

 

Back To Question