Power of 2 Java

PROGRAM TO FIND WHETHER A NUMBER IS POWER OF TWO




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


OUTPUT:
No
Yes

Comments

Popular posts from this blog

Solve the Sudoku Python

Solve the Sudoku Java

Find Duplicates Java