Answer by Matt Howells for How to check if a number is a power of 2
bool IsPowerOfTwo(ulong x){ return x > 0 && (x & (x - 1)) == 0;}
View ArticleAnswer by user134548 for How to check if a number is a power of 2
private static bool IsPowerOfTwo(ulong x){ var l = Math.Log(x, 2); return (l == Math.Floor(l));}
View ArticleAnswer by Andreas Petersson for How to check if a number is a power of 2
return (i & -i) == i
View ArticleAnswer by Michael Burr for How to check if a number is a power of 2
Some sites that document and explain this and other bit twiddling hacks...
View ArticleAnswer by configurator for How to check if a number is a power of 2
After posting the question I thought of the following solution:We need to check if exactly one of the binary digits is one. So we simply shift the number right one digit at a time, and return true if...
View ArticleAnswer by Greg Hewgill for How to check if a number is a power of 2
There's a simple trick for this problem:bool IsPowerOfTwo(ulong x){ return (x & (x - 1)) == 0;}Note, this function will report true for 0, which is not a power of 2. If you want to exclude that,...
View ArticleHow to check if a number is a power of 2
Today I needed a simple algorithm for checking if a number is a power of 2.The algorithm needs to be:SimpleCorrect for any ulong value.I came up with this simple algorithm:private bool...
View ArticleAnswer by vivek nuna for How to check if a number is a power of 2
It's very easy in .Net 6 now.using System.Numerics;bool isPow2 = BitOperations.IsPow2(64); // sets trueHere is the documentation.
View ArticleAnswer by Uche Igbokwe for How to check if a number is a power of 2
There is a one liner in .NET 6// IsPow2 evaluates whether the specified Int32 value is a power of two.Console.WriteLine(BitOperations.IsPow2(128)); // True
View ArticleAnswer by Yanara for How to check if a number is a power of 2
There were a number of answers and posted links explaining why the n & (n-1) == 0 works for powers of 2, but I couldn't find any explanation of why it doesn't work for non-powers of 2, so I'm...
View ArticleAnswer by Dr.jacky for How to check if a number is a power of 2
Kotlin:fun isPowerOfTwo(n: Int): Boolean { return (n > 0) && (n.and(n-1) == 0)}orfun isPowerOfTwo(n: Int): Boolean { if (n == 0) return false return (n and (n - 1).inv()) == n}inv inverts...
View ArticleAnswer by velocity for How to check if a number is a power of 2
I've been reading the documentation for Random.nextInt(int bound) and saw this nice piece of code which checks whether the parameter is a power of 2, which says (part of the code) :if ((bound &...
View ArticleAnswer by Golden Lion for How to check if a number is a power of 2
try this function which uses mod 2def is_power_of_two(n): if n == 0: return False while n != 1: if n % 2 != 0: return False n = n // 2 return True
View ArticleAnswer by Cato for How to check if a number is a power of 2
I'm assuming 1 is a power of two, which it is, it's 2 to the power of zero bool IsPowerOfTwo(ulong testValue) { ulong bitTest = 1; while (bitTest != 0) { if (bitTest == testValue) return true; bitTest...
View ArticleAnswer by RARE Kpop Manifesto for How to check if a number is a power of 2
Apparently if you wanna use something other than bit-masking, one can also efficiently determine a power of 2 as a side feature of the Fermat primality testjot 1048576 | gawk -Mbe '($++NF = 2^((n =...
View ArticleAnswer by Rushil Patel for How to check if a number is a power of 2
unsigned int n; // we want to see if n is a power of 2bool f; // the result goes here f = (n & (n - 1)) == 0;Note that 0 is considered a power of 2 here. To remedy this, use:f = n && !(n...
View ArticleAnswer by RARE Kpop Manifesto for How to check if a number is a power of 2
Here's a clean approach if you want to avoid using any and all bitwise ops (this is generic pseudo-code instead of being exactly C#) :func is_power_of_2( uint64 n ) { return (n > 4) ? 2**63 % n == 0...
View ArticleAnswer by Hans Brende for How to check if a number is a power of 2
Many answers solve this cleverly for integers, but here's how, using only basic floating-point operations, to solve it for double!const double HALF_EPS = 1.1102230246251565e-16; // 2^-53bool...
View Article