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

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

$
0
0

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 == (x|(x-1)) +1));}

Viewing all articles
Browse latest Browse all 38

Trending Articles



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