Quantcast
Channel: How to check if a number is a power of 2 - Stack Overflow
Browsing latest articles
Browse All 34 View Live

Answer by Aleksandar Biševac for How to check if a number is a power of 2

This one returns if the number is the power of two up to 64 value ( you can change it inside for loop condition ("6" is for 2^6 is 64);const isPowerOfTwo = (number) => { let result = false; for (let...

View Article



Answer by ABHISHEK PARMAR for How to check if a number is a power of 2

in this approach , you can check if there is only 1 set bit in the integer and the integer is > 0 (c++).bool is_pow_of_2(int n){ int count = 0; for(int i = 0; i < 32; i++){ count += (n>>i...

View Article

Answer by Marcus Moo for How to check if a number is a power of 2

This is another method to do it as wellpackage javacore;import java.util.Scanner;public class Main_exercise5 { public static void main(String[] args) { // Local Declaration boolean ispoweroftwo =...

View Article

Answer by Anil Gupta for How to check if a number is a power of 2

I see many answers are suggesting to return n && !(n & (n - 1)) but to my experience if the input values are negative it returns false values.I will share another simple approach here since...

View Article

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

In C, I tested the i && !(i & (i - 1) trick and compared it with __builtin_popcount(i), using gcc on Linux, with the -mpopcnt flag to be sure to use the CPU's POPCNT instruction. My test...

View Article


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

Mark gravell suggested this if you have .NET Core 3, System.Runtime.Intrinsics.X86.Popcnt.PopCountpublic bool IsPowerOfTwo(uint i){ return Popcnt.PopCount(i) == 1}Single instruction, faster than (x !=...

View Article

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

Improving the answer of @user134548, without bits arithmetic:public static bool IsPowerOfTwo(ulong n){ if (n % 2 != 0) return false; // is odd (can't be power of 2) double exp = Math.Log(n, 2); if (exp...

View Article

Answer by FReeze FRancis for How to check if a number is a power of 2

for any power of 2, the following also holds.n&(-n)==nNOTE: fails for n=0 , so need to check for itReason why this works is:-n is the 2s complement of n. -n will have every bit to the left of...

View Article


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

The following addendum to the accepted answer may be useful for some people:A power of two, when expressed in binary, will always look like 1 followed by n zeroes where n is greater than or equal to 0....

View Article


Answer by Khaled.K for How to check if a number is a power of 2

Example0000 0001 Yes0001 0001 NoAlgorithmUsing a bit mask, divide NUM the variable in binaryIF R > 0 AND L > 0: Return FALSEOtherwise, NUM becomes the one that is non-zeroIF NUM = 1: Return...

View Article

Answer by Raz Megrelidze for How to check if a number is a power of 2

bool IsPowerOfTwo(int n) { if (n > 1) { while (n%2 == 0) { n >>= 1; } } return n == 1; }And here's a general algorithm for finding out if a number is a power of another number. bool...

View Article

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

Here is another method I devised, in this case using | instead of & :bool is_power_of_2(ulong x) { if(x == (1 << (sizeof(ulong)*8 -1) ) return true; return (x > 0) && (x<<1...

View Article

Answer by Prakash Jat for How to check if a number is a power of 2

return ((x != 0) && !(x & (x - 1)));If x is a power of two, its lone 1 bit is in position n. This means x – 1 has a 0 in position n. To see why, recall how a binary subtraction works. When...

View Article


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

int isPowerOfTwo(unsigned int x){ return ((x != 0) && ((x & (~x + 1)) == x));}This is really fast. It takes about 6 minutes and 43 seconds to check all 2^32 integers.

View Article

Answer by bugs king for How to check if a number is a power of 2

bool isPowerOfTwo(int x_){ register int bitpos, bitpos2; asm ("bsrl %1,%0": "+r" (bitpos):"rm" (x_)); asm ("bsfl %1,%0": "+r" (bitpos2):"rm" (x_)); return bitpos > 0 && bitpos == bitpos2;}

View Article


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

A number is a power of 2 if it contains only 1 set bit. We can use this property and the generic function countSetBits to find if a number is power of 2 or not.This is a C++ program:int...

View Article

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

bool isPow2 = ((x & ~(x-1))==x)? !!x : 0;

View Article


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

Here's a simple C++ solution:bool IsPowerOfTwo( unsigned int i ){ return std::bitset<32>(i).count() == 1;}

View Article

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

Find if the given number is a power of 2. #include <math.h>int main(void){ int n,logval,powval; printf("Enter a number to find whether it is s power of 2\n"); scanf("%d",&n);...

View Article

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

Browsing latest articles
Browse All 34 View Live




Latest Images