Quantcast
Channel: How to check if a number is a power of 2 - Stack Overflow
Browsing all 38 articles
Browse latest View live

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 Article


Answer 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 Article


Answer by Andreas Petersson for How to check if a number is a power of 2

return (i & -i) == i

View Article

Answer 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 Article

Answer 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 Article


Answer 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 Article

How 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 Article

Answer 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 Article


Answer 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 Article


Answer 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 Article

Answer 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 Article

Answer 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 Article

Answer 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 Article


Answer 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 Article

Answer 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 Article


Answer 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 Article

Answer 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 Article


Answer 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
Browsing all 38 articles
Browse latest View live


Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>