Count the number of 1(MIT hackmem)
2010-09-02 15:36
477 查看
MIT Hackmem
int bitcount(unsigned int n)
{
/* works for 32-bit numbers only */
/* fix last line for 64-bit numbers */
register unsigned int tmp;
tmp = n - ((n >> 1) & 033333333333) - ((n >> 2) & 011111111111);
return ((tmp + (tmp >> 3)) & 030707070707) % 63;
}
首先,可以把 n 看成是8进制的数,即把 n 三位三位的分开,每位是一个bit。
一个三bit的数字可以看成是abc,它的和为4a+2b+c。如果我们将这个数右移一位,即得到ab,其和为2a+b。将原来的n减去右移一位后的n,得到和为2a+b+c。如果我们在把n右移一位,即把原来的n 右移两位的话,我们将得到一个a,然后再用前面减法的结果2a+b+c减去右移两位后的n 的话,得到和为a+b+c,如下面所示:
a b c n1
右移一位 a b n2
再右移一位 a n3
n=n1;
n'=n1-n2;
n''=n'-n3
=n1-n2-n3
=4a+2b+c -(2a+b) -a
=a+b+c;
tmp = n - ((n >> 1) & 033333333333) - ((n >> 2) & 011111111111);
即将每三位的和加到末尾上,其余位为0。
然后,程序return了一个值。
return ((tmp + (tmp >> 3)) & 030707070707) % 63;
这里它是将tmp看成是有六bit数组成的32位数,当然这个是从最低有效位算起的,将每六位数的和加到末尾上,然后用63求余(这里用63求余,是因为1000000是64,如果将tmp看成是由六bit数组成的话,高于63就有一个1,所以可以用取余的方法),最后得到共含有的1的个数。
对于64-bit的数而言,我们可以用3倍的八进制数(即3bit位),即1023。
int bitcount(unsigned int n)
{
/* works for 32-bit numbers only */
/* fix last line for 64-bit numbers */
register unsigned int tmp;
tmp = n - ((n >> 1) & 033333333333) - ((n >> 2) & 011111111111);
return ((tmp + (tmp >> 3)) & 030707070707) % 63;
}
首先,可以把 n 看成是8进制的数,即把 n 三位三位的分开,每位是一个bit。
一个三bit的数字可以看成是abc,它的和为4a+2b+c。如果我们将这个数右移一位,即得到ab,其和为2a+b。将原来的n减去右移一位后的n,得到和为2a+b+c。如果我们在把n右移一位,即把原来的n 右移两位的话,我们将得到一个a,然后再用前面减法的结果2a+b+c减去右移两位后的n 的话,得到和为a+b+c,如下面所示:
a b c n1
右移一位 a b n2
再右移一位 a n3
n=n1;
n'=n1-n2;
n''=n'-n3
=n1-n2-n3
=4a+2b+c -(2a+b) -a
=a+b+c;
tmp = n - ((n >> 1) & 033333333333) - ((n >> 2) & 011111111111);
即将每三位的和加到末尾上,其余位为0。
然后,程序return了一个值。
return ((tmp + (tmp >> 3)) & 030707070707) % 63;
这里它是将tmp看成是有六bit数组成的32位数,当然这个是从最低有效位算起的,将每六位数的和加到末尾上,然后用63求余(这里用63求余,是因为1000000是64,如果将tmp看成是由六bit数组成的话,高于63就有一个1,所以可以用取余的方法),最后得到共含有的1的个数。
对于64-bit的数而言,我们可以用3倍的八进制数(即3bit位),即1023。
相关文章推荐
- How to count the occurrences of a number?
- leetcode:count the number of nodes
- count the number of "1" in the binary figure
- Q9.11 count the number of ways of parenthesizing the expression
- 三角形的数目 Count the number of possible triangles
- Count the number of bits that are on in an unsigned integer(计算一个无符整数中1Bit的个数)-- (1)
- Error Number : 1547 Error Message: Column count of mysql.proc is wrong. Expected 20, found 16. The
- Count the number of alphabets, spaces, digits and other characters in one line character
- quick count the number of the rows in a big table
- [geeksforgeeks] Count the number of occurrences in a sorted array
- COJ 1068: Count the Number of Cycles(向量点积)
- Write a method to count the number of 2s between 0 and n
- Regex 101 Exercise I9 - Count the number of matches
- *Count the number of possible triangles
- 在排序数组中的出现次数 Count the number of occurrences in a sorted array
- 非零比特的个数 count the number of bits
- Count the number of occurrences in a sorted array
- Give a very good method to count the number of ones in a "n" (e.g. 32) bit number.
- FB面经prepare: Count the number of Vector
- OpenCV count the number of connected camera 检测连接的摄像头的数量