In this Java Programming tutorial you will learn how to check if number is Power of two using bitwise operator. Main purpose or this program is to teach you how to use bit-wise operators like bitwise AND (&) in Java. A number is said to be power of two if all its prime factors are 2, but in binary world things works little differently. If you have read
hacker's delight book then you know that there are several technique to check if a number is power of two or not, one of them is performing bit-wise AND operation between
number and
number -1, if result is zero then number is power of two. Just remember, here we are doing binary subtraction and not decimal one. In bitwise operator, each bit is used in evaluation for example if you use bitwise AND then each bit of both operand will go through AND operation and result will contain 1 only if both bits are one otherwise zero. I will explain how exactly this program work, but let's first see the program itself.