Quickly determine whether a number is a power of 2, if so, and determine how many times the square!
when the power of 2 is written in binary form, it is easy to find a feature: The binary is only a 1, and 1 followed by n 0, so the problem can be converted to determine whether 1 followed by n 0 on it.
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:
The code that uses the recursive implementation is as follows:
#include "stdio.h"
#include "stdlib.h"
int log2 (int value) //recursion to determine how many times a number is 2
(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 of 2,
printf ("%d is not a power of 2!") \ n ", num);
else
printf ("%d is 2%d times!") \ n ", num,log2 (num));
System ("pause");
return 0;
}
The code that uses the non-recursive implementation is as follows:
#include "stdio.h"
#include "stdlib.h"
int log2 (int value) //non-recursive judgment of how many times a number is 2 of the square
{
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 of 2,
printf ("%d is not a power of 2!") \ n ", num);
else
printf ("%d is 2%d times!") \ 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:
int Func3 (int data)
{ ////data& (Data-1) removes the rightmost 1 each time, removes how many 1, or contains several 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.
Quickly determine whether a number is a power of 4, if so, and determine how many times the square!
after the power of 4 is written in binary form, it's easy to find a feature: binary only has a 1 (1 in odd position), and 1 followed by an even number of 0, so the problem can be converted to determine whether 1 followed by an even number of 0 on it.
4 of the integer power of the binary numbers are (4) 100, (16) 10000, (64) 1000000 ...
In addition, the power of the 4 square 4^n can also be written as 2^ (2*n), which can also be written as a power of 2, of course, to meet the power of 2 of the conditions, that is, Num & num-1==0.
Idea: First use conditional num & num-1==0 to judge whether the power of 2, if not satisfied, is not. If satisfied, in the condition of Num & 0x55555555 to judge, if true, then this integer is 4 power side, otherwise not.
The code that uses the recursive implementation is as follows:
#include "stdio.h"
#include "stdlib.h"
bool fn (unsigned int x) //Determine if x is a power exponent of 4
{
if (x & (X-1)) c6/>//to determine whether X is 2 power square return
false;
return x & 0x55555555; To determine whether 1 is in odd places
}
int log4 (int value) ///recursive to determine how many times a number is 4 of the second party
{
if (value = = 1) return
0;
else
{
value>>=1; Shift to right return
1+log4 (value>>1); Shift to right
}
int main (void)
{
int num;
printf ("Please enter an integer:");
scanf ("%d", &num);
if (FN (num)) //use and operation to determine whether a number is 2 power to
printf ("%d is 4%d Times Square!") \ n ", Num,log4 (num));
else
printf ("%d is not a power party of 4!") \ n ", num);
System ("pause");
return 0;
}
The code that uses the non-recursive implementation is as follows:
#include "stdio.h"
#include "stdlib.h"
bool fn (unsigned int x) //Determine if x is a power exponent of 4
{
if (x & (X-1)) c6/>//to determine whether X is 2 power square return
false;
return x & 0x55555555; To determine whether 1 is in odd positions
}
int log4 (int value) //Non-recursive judge how many times a number is 4 of the second party
{
int x=0;
while (value>1)
{
value>>=1; Shift Right
value>>=1;
x + +;
}
return x;
}
int main (void)
{
int num;
printf ("Please enter an integer:");
scanf ("%d", &num);
if (FN (num)) //use and operation to determine whether a number is 2 power to
printf ("%d is 4%d Times Square!") \ n ", Num,log4 (num));
else
printf ("%d is not a power party of 4!") \ n ", num);
System ("pause");
return 0;
}