面试题1
大耳 2006-08-16 10:51:52 1 What is the fastest way to count the number of ones in a given number represented in a
binary form. You can assume that you have infinte run time memory and the response time
should be O(1) always. Can you optimize the memory used?
答案:
int bits_set( int word )
{
int tmp;
tmp = (word >> 1) & 033333333333;
tmp = word - tmp - ((tmp >> 1) & 033333333333);
return (((tmp + (tmp >> 3)) & 030707070707) % 077);
}
有人分析一下么?或者给个更好的解答:)