How do you determine whether a number is a power exponent of 2? If so, and how many times is it judged? _c language

Source: Internet
Author: User
After the power of 2 is written in binary form, it is easy to find a feature: there is only one 1 in the binary, and 1 followed by n 0, so the question can be converted to determine whether 1 followed by n 0.
If you subtract this number by 1, you will find that the only 1 will become 0, and the original n 0 will be 1, so that the original number and the number after subtracting 1 will be zero.
The quickest way:
(number & number-1) = = 0
Reason:Because 2 of the n-th conversion is binary to 10 ... 0 such a form (excluding 0). With the digits on their own-1, this gets the result of 0. For example. The binary of 8 is the binary of 1000;8-1=7,7 is 111. The result of the two phase is 0. The calculations are as follows:
1000
& 0111
-------
0000
the code that uses the recursive implementation is as follows:
Copy Code code as follows:

#include "stdio.h"
#include "Stdlib.h"
int log2 (int value)//recursive judge how many times a number is 2
{
if (value = = 1)
return 0;
Else
Return 1+log2 (value>>1);
}
int main (void)
{
int num;
printf ("Please enter an integer:");
scanf ("%d", &num);
if (num& (num-1))//use and operation to determine whether a number is a power exponent of 2
printf ("%d is not a power party of 2!") \ n ", num);
Else
printf ("%d is 2%d Times Square!") \ n ", num,log2 (num));
System ("pause");
return 0;
}

The code that uses the non-recursive implementation is as follows:
Copy Code code as follows:

#include "stdio.h"
#include "Stdlib.h"
int log2 (int value)//non-recursive judge how many times a number is 2
{
int x=0;
while (value>1)
{
value>>=1;
x + +;
}
return x;
}
int main (void)
{
int num;
printf ("Please enter an integer:");
scanf ("%d", &num);
if (num& (num-1))//use and operation to determine whether a number is a power exponent of 2
printf ("%d is not a power party of 2!") \ n ", num);
Else
printf ("%d is 2%d Times Square!") \ n ", num,log2 (num));
System ("pause");
return 0;
}

Extension: Find the number of 1 in binary of N.
A very ingenious use of a property,n=n& (n-1) can remove the property of the rightmost 1 in the binary of N, and the loop is removed until all 1 is removed, which reduces the complexity of the problem to only the number of 1. The code is as follows:
Copy Code code as follows:

int Func3 (int data)
{///using data& (data-1) can remove the rightmost 1 each time, remove how many 1, that is, contains a few 1
int count = 0;
while (data)
{
data = data & (DATA-1);
count++;
}
return count;
}

Extended Question two:
How many bits are different in the binary of A and B. This problem can be divided into two steps, (1) different from A and B or get C, i.e. C=a^b, (2) to calculate how many 1 in the C binary.
Related Article

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.