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?



January 01, 2006 23:57:26 #4
 prakash   Member Since: Visitor    Total Comments: N/A 

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;

}

     

 

Back To Question