Sign in to follow this  
osh

Power of 2

Recommended Posts

osh    116
Hello all! I'd like to ask what is most effective way to check if the given number is power of 2? Thx

Share this post


Link to post
Share on other sites
snk_kid    1312
Quote:
Original post by deks
bool bIsPowerOfTwo = !(n&(n-1));

=[dEkS]=


slight mod:


bool is_pow_of2 = (n != 0 && !(n & (n - 1)));

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this