Sol 1:
Simple Iterative solution.(Very slow)
int countbits (unsigned int number) {
int count = 0;
while (number) {
count += number & 0x1u;
number >>= 1;
}
return count;
}
Sol 2:
Faster Solution for 32 bit machines.
compute 16bits then add then to return full number of ones in 32 bit number.
static char ones_in_16bit_num [0x1u << 16] ;
int countbits (unsigned int number)
{
return ones_in_16bit_num [number & 0xffffu] + ones_in_16bit_num [(number >> 16) & 0xffffu] ;
}
Categories
FollowersBlog Archive
Jobs
Find more freelance jobs
|
Sunday, March 28, 2010
Subscribe to:
Posts (Atom)
|
Popular Posts
|