Fast computing of 1 digits in 32 digits
Sailor_forever sailing_9806@163.com reprint please note
Http://blog.csdn.net/sailor_8318/archive/ 2008/10/12 /3059935. aspx
A statement is used to determine whether X is a power of 2.
# Define is2 * n (x) (X & (X-1 ))? 0: 1)
Int main (void)
{
Int M = 512;
Cout <(M & (m-1 ))? False: True) <Endl;
// That is, when only one digit in M is 1, it is several power values.
// If there are two or more 1 s, the (M & (m-1) value is not 0, and the output value is 0, indicating that it is not several power of 2
Return (0 );
}
Analogy: The power of X 2 indicates that the number of digits of 1 in X is 1, and the question is converted to the number of digits of 1 in a 32-digit number. If it is 1, indicates several power pairs of 2.
Similarly, you can use this rule to calculate the number of 1 digits in a 32-digit number. (M & (m-1) can be used to remove one digit each time. The calculated number of times is the number of 1 digits.