In "The beauty of programming" there is a section on how to find a byte of unsigned integer variable binary representation of the number of 1 , mainly refers to four methods.
Here's a quick introduction:
1. method of finding redundancy
when converting a decimal number to a binary number, use the exception 2 take the remainder method. saves the remainder that is obtained every time except 2 the binary representation of the decimal integer is the inverse output. Therefore, this method can be used to count the number of 1.
int count (unsigned char n)
{
int sum=0;
while (n)
{
if (n%2==1)
sum++;
n/=2;
}
return sum;
}
2. bit arithmetic
we know that computers are much faster in processing bit operations, so it is possible to consider the method of bit operation. Each time with 0x01 and operation, if not 0, then thecounter plus 1, and then move to the right 1 bits, Cycle the process.
int count (unsigned char n)
{
int sum=0;
while (n)
{
sum+=n&0x01;
n>>=1;
}
return sum;
}
3. Fast Method
The method described in 2 has a cycle count of 8, and there is a way to reduce the number of cycles. is to use minus 1 to proceed with the operation, so that each time, there will be a 1 less .
Like what:0010 0110minus1Get0010 0101 &0010 0110equals0010 0100.The reason for this isR1r2...rn, if the last bit1the one forRK, the number is reduced1after the binary representationRKmust be0, butR (k+1) ... rnthen all are1, with the original number to proceed with the operation will not impressRKthe front1, so each time you do this, you can eliminate a binary1.
int count (unsigned char n)
{
int sum=0;
while (n)
{
N&= (n-1);
sum++;
}
return sum;
}
4. Check the table method.
Therefore, a byte of unsigned integer data range is between [0,255] , so you can directly define a length of the array table[0-255], the number of 1 in the 0-255 binary representation of 1 is assigned to the elements of the array, so that the lookup is done directly.
int table[256]={
0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8,
};
int count (unsigned char n)
{
return table[n];
}
The above are four different methods, if you require a three -bit unsigned integer containing 1 of the number, you can change according to the fourth method:
int table[256]={
0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8,
};
int count (unsigned int n)
{
unsigned char *p= (unsigned char *) &n;
Return table[*p]+table[* (p+1)]+table[* (p+2)]+table[* (p+3)];
}
Find the number of 1 in binary