Program to find whether a no is power of two | Java program to efficiently check for power for 2 ; Method to check if x is power of 2 ; First x in the below expression is for the case when x is 0 ; Driver method
class Test { static boolean isPowerOfTwo ( int x ) { return x != 0 && ( ( x & ( x - 1 ) ) == 0 ) ; } public static void main ( String [ ] args ) { System . out . println ( isPowerOfTwo ( 31 ) ? " Yes " : " No " ) ; System . out . println ( isPowerOfTwo ( 64 ) ? " Yes " : " No " ) ; } }