## Bug in checking positive integer N for power of 2 with large N

check if number is power of 2 in java

check if number is power of 2 javascript

matlab check if number is power of 2

c++ check if number is power of 2

convert number to power of 2

bitwise power of 2

power of 2 c++

I have this program to check if a no is power of 2 or not, but it's not working on a test case if `no = 1099511627776`

:

int not; // no of testcase unsigned long long int no; // input number int arr[100]; scanf ("%d", ¬); for ( int i = 0; i < not; i++ ) { scanf ("%llu", &no); if ( no & !(no & (no - 1))) // check wheather no is power of 2 or not excludig 0 arr[ i ] = 1; else arr[ i ] = 0; } for ( int i = 0; i < not; i++ ) { if ( arr[ i ] == 1 ) printf ("YES\n"); else printf ("NO\n"); }

**How to Check If a Number Is a Power of Two,** That's easy if it's in the form 2^n, where n is an integer. But what about arbitrary positive numbers like 16392, 524288, or 0.00390625? Are they Given a positive integer, write a function to find if it is a power of two or not. Examples : Input : n = 4 Output : Yes 2 2 = 4 Input : n = 7 Output : No Input : n = 32 Output : Yes 2 5 = 32

`&`

is bitwise AND. You want logical AND: `no && !(no & (no - 1))`

.

**Ten Ways to Check if an Integer Is a Power Of Two in C,** Decimal-Based Approaches to Checking for Powers of Two. Decimal integers The log in base 2 of x, or log2(x), is the exponent n to which 2 is raised to get x. Mathematically The first half of the expression ensures that x is a positive integer. The second a) chip and compiler can make a BIG difference power_of_2 = 1 while power_of_2 <= n: if power_of_2 == n: return True power_of_2 *= 2 return False simply compares your number to all smaller-or-equal powers of two.

Here are my few observation from the code you mentioned.

**Firstly**, What if user given `not`

value more than `100`

? It causes **undefined behavior** if `not>100`

as you declared `arr`

as size of `100`

. To avoid this one way is first scan `not`

and then create array equal to `not`

size. for e.g

int not = 0; // no of testcase scanf ("%d", ¬); int arr[not]; /* create array equal to not size.. */

Or create array dynamically like

int not = 0; // no of testcase scanf ("%d", ¬); int *arr = malloc(not * sizeof(*arr)); /* create dynamic array equal to not size.. */

**Secondly**, to check given number is power of 2 or not, this `!(no & (no - 1))`

is correct but to exclude `zero`

i.e if given input `no`

is `0`

then you shouldn't check `!(no & (no - 1))`

this. for this use logical AND `&&`

operator. This

if ( no & !(no & (no - 1))) { }

should be

if ( no && !(no & (no - 1))) { }

**Integer overflow,** In computer programming, an integer overflow occurs when an arithmetic operation attempts to above for an N-bit integer, an overflow reduces the result to modulo N-th power of 2, to a large positive value (for example, 8-bit integer addition 255 + 2 results in 1, Overflow bugs are evident in some computer games. First Run: Enter an integer number: 32 32 is a number that is the power of 2. Second Run: Enter an integer number: 36 36 is not the power of 2.

**Check if a given number N is a power of k,** def check_pow(N, k): if k < 0 or N < 0: raise ValueError("k and N must be greater change it to a=logkN. And so if a is an integer, then you know that N is a power of k. A binary search between the last smaller power of 2 and its -1 and 1, it will always approach zero rather than getting larger, so you'll This function uses logarithms to determine if x is a power of two, put perhaps not in the way you’d expect. The log in base 2 of x, or log 2 (x), is the exponent n to which 2 is raised to get x. Mathematically, if n is an integer, then x is a power of two; if n is not an integer, then x is not.

**Checking if a number is power of 2 or not,** Since Java 7, you should use try-with-resources on your Scanner for safe and efficient technique independent of the representation of the number (perhaps: if(n % 2 != If you want to use binary numbers you can check that the number is positive and contains If that number is 1 then the original number is a power of 2. The program below takes two integers from the user (a base number and an exponent) and calculates the power. For example: In case of 2 3 . 2 is the base number. 3 is the exponent. And, the power is equal to 2*2*2.

**Program to find whether a no is power of two,** a no is power of two. Given a positive integer, write a function to find if it is a power of two or not. Examples : Input : n = 4 Output : Yes 22 = 4 Input : n = 7 Output : No Input : n = 32 Output : Yes 25 = 32 Function to check if x is power of 2. The idea is to find a number mod 5 n 5^n 5 n and mod 2 n, 2^n, 2 n, and then combine those results, using the Chinese remainder theorem, to find that number mod 1 0 n 10^n 1 0 n. Find the last two digits of 7 4 540 74^{540} 7 4 5 4 0 .

##### Comments

- Note that while the word
`not`

isn't part of stock C, it is an "alternative token" for`!`

in the C standard, and also reserved in standard C++. So if you ever might want to compile your C code as C++, or use those alternative tokens (which I personally prefer to the likes of`!`

and`&&`

and`||`

) then you might think about avoiding calling variables "not". That's all in addition to the fact that it's a confusing name for a variable...