Program to find whether a given number is power of 2 | Java program of the above approach ; Function to check if x is power of 2 ; Driver code
import java . io . * ; class GFG { static boolean isPowerofTwo ( int n ) { if ( n == 0 ) return false ; if ( ( n & ( ~ ( n - 1 ) ) ) == n ) return true ; return false ; } public static void main ( String [ ] args ) { if ( isPowerofTwo ( 30 ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; if ( isPowerofTwo ( 128 ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }