Logarithm tricks for Competitive Programming | Java implementation to check that a integer is a power of Two ; Function to check if the number is a power of two ; Driver Code
import java . lang . Math ; class GFG { public static boolean isPowerOfTwo ( int n ) { return ( Math . ceil ( Math . log ( n ) / Math . log ( 2 ) ) == Math . floor ( Math . log ( n ) / Math . log ( 2 ) ) ) ; } public static void main ( String [ ] args ) { int N = 8 ; if ( isPowerOfTwo ( N ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }