Qualified
I just added one more on the homepage.ArticleTo determine whether a number is the N power of 2.
You have contributed a lot of ideas.
After sorting out, there are so many of the following.
I used to perform tests, but now I have done everything in a mess and I have never been so eager to learn.AlgorithmYou cannot write good algorithms to entertain everyone.
The ME algorithm is weak.
However, the habit of picking me is not corrected. Therefore, it is very high to start analyzing others'Code.
Let's analyze several typical codes that you have replied to (if they are running correctly, we will not analyze them ):
1 macro definition.
# Define ispow2 (x) & (x-1 )? False: True
I just wrote an article to prove that he has some flaws. That is, when X is 0,
We know that 0 is not the Npower of 2. Simply put, log () has no value.
For this judgment, the bitwise AND of 0 and-1 must be 0. Actually, the judgment can pass.
This is not too much.
Of course, this algorithm is quite good. At least I was shocked when I saw it myself. After all, this is the first time you see it.
=. =
Don't laugh at me... This is my first time.
2nd floor 7 Revised Version
Public Static Bool Floor_7 ( Int Num)
{
If (Num <= 1 )
{
Return False ;
}
Else
{
Return (Num & (Num - 1 )) = 0 ) ? True : False ;
}
}
On the 7th floor, it seems that I realized this problem and I made a judgment.
Unfortunately, he judges another one. We know that 1 is the power 0 of 2. 1 should meet the requirements.
Revised version on the third and eighth floor
Public Bool Floor_8 ( Int N)
{
If (N < 0 )
Throw New Invalidoperationexception ();
If (N < 2 )
Return False ;
ReturnN&(N- 1)= 0;
}
On the eighth floor, I realized the negative number problem. Unfortunately, he has the same problem with the 7 th floor. However, this is only a small error.
Log algorithm on the 22nd floor
Public Bool Floor_22 ( Int X)
{
Float RET = Log (X) / Log ( 2 );
Return ABS (( Int ) RET - RET) <= 0.00001 ;
}
The logarithm algorithm on the 22nd floor is interesting. Unfortunately, the floating point error is not an easy problem to avoid.
Because floating point numbers cannot be directly compared, A 0.00001 scale is used.
So there is a problem: what happens when X is large?
I found an abnormal number to test:
Zero X 10000001
The result is true. Because the decimal part of the result is too small.
=. =
(Do I have a mental abnormality .. )
Another counterexample is to overturn the idea of an algorithm.
Algorithms on the 5th floor and 37 floors.
Public Static Bool Floor_37 ( Int Num)
{
Double Result = Math. Log (Num, 2 );
If (Result. tostring (). indexof ( " . " ) > = 0 )
{
Return False ;
}
Else
{
Return True ;
}
}
The same problem. If log is used, the loss of precision of floating point numbers cannot be avoided. This is impossible.
So I have summarized the (x) & (x-1) algorithm has not been proved, I do not know there is no other inverse examples except 0.
After all, this formula does not strictly prove the process. For more information, see my other article:
Http://www.cnblogs.com/ArthasCui/archive/2011/03/03/1969935.html
Therefore, I think the most safe thing is bitwise calculation. It depends on the number of 1, which is the most practical.
Of course, there is a negative number here. The first is 1, and the remaining is 0. However, a smart reviewer provides a powerful way to avoid negative use cases:
The parameter type is uint!
Okay, you won.
I just saw the best one:
Public Bool Is _( Int Num)
{
Switch (Num)
{
Case Zero X 0002 :
Case Zero X 0004 :
Case Zero X 0008 :
Case Zero X 0010 :
Case Zero X 0020 :
Case Zero X 0040 :
Case Zero X 0080 :
Case Zero X 0100 :
Case Zero X 0200 :
Case Zero X 0400 :
Case Zero X 0800 :
Case Zero X 1000 :
Case Zero X 2000 :
Case Zero X 4000 :
Case Zero x 8000 :
Return True ;
Default :
Return False ;
}
}
If I add 0x0001, I think it's perfect...
Of course, I want to continue writing this article. It is indeed a tiring one.