How can I determine whether a number is a power of 2? If so, how much power is determined?

Source: Internet
Author: User

After the power of 2 is written in binary form, it is easy to find that there is only one 1 in binary, and 1 followed by n 0; therefore, the problem can be converted to determining whether n zeros are followed by 1.
If we subtract this number from 1, we will find that the only one will change to 0, and the original n 0 will change to 1; therefore, the original number and the number after the first subtraction will be zero after calculation.
The fastest way:
(Number & number-1) = 0
Cause:Because the Npower of 2 is converted to binary 10 ...... 0 (except 0 ). And the number of digits-1 on the top. The result is 0. For example. The binary value of 8 is 1000, and the binary value of 8-1 = 7 is 111. The result of the two phases is 0. The calculation is as follows:
1000
& 0111
-------
0000
The Code implemented by recursion is as follows:
Copy codeThe Code is as follows: # include "stdio. h"
# Include "stdlib. h"
Int log2 (int value) // recursively determines the power of a number 2
{
If (value = 1)
Return 0;
Else
Return 1 + log2 (value> 1 );
}
Int main (void)
{
Int num;
Printf ("enter an integer :");
Scanf ("% d", & num );
If (num & (num-1) // use and calculate to determine whether a number is a power of 2
Printf ("% d is not the power of 2! \ N ", num );
Else
Printf ("% d is the power of % d of 2! \ N ", num, log2 (num ));
System ("pause ");
Return 0;
}

The Code implemented using non-recursion is as follows:Copy codeThe Code is as follows: # include "stdio. h"
# Include "stdlib. h"
Int log2 (int value) // determines the power of a number 2 in non-recursion.
{
Int x = 0;
While (value> 1)
{
Value >>> = 1;
X ++;
}
Return x;
}
Int main (void)
{
Int num;
Printf ("enter an integer :");
Scanf ("% d", & num );
If (num & (num-1) // use and calculate to determine whether a number is a power of 2
Printf ("% d is not the power of 2! \ N ", num );
Else
Printf ("% d is the power of % d of 2! \ N ", num, log2 (num ));
System ("pause ");
Return 0;
}

Extended: calculates the number of 1 in a number n binary.
N = n & (n-1) can remove the rightmost 1 Property in n's binary, until all 1 is removed, this method reduces the complexity of the problem to only one number. The Code is as follows:Copy codeThe Code is as follows: int Func3 (int data)
{// Use data & (data-1) to remove the rightmost 1 at a time. How many 1 s are removed, that is, several 1 s are included.
Int count = 0;
While (data)
{
Data = data & (data-1 );
Count ++;
}
Return count;
}

Expansion Question 2:
The number of bits in the binary format of A and B is different. This problem can be divided into two steps: (1) xor a and B to obtain C, that is, C = A ^ B, (2) calculate the number of 1 in the binary of C.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.