Check Whether number is power of Two using bit wise operation in Java

Check formatted code and output - https://code.hackerearth.com/b48d0aG

import java.util.Scanner;
class TestClass {
    public static void main(String args[] ) throws Exception {
     
        Scanner sc = new Scanner(System.in);
     
        int number = sc.nextInt();
        for(int i=0;i<number;i++)
         {
             if(isPowerOfTwo(i)){
              System.out.println(" "+i );
             }
         }
     }
 
     static boolean isPowerOfTwo(int x)   {
        if(x==0)  return false;
        int numberofones =count_one(x & (x - 1));
        return numberofones==0?true:false;
    }
 
    static int count_one (int n)
        {
            int count=0;
            while( n!=0 )
            {
            n = n&(n-1);
               count++;
            }
            return count;
    }
}

Comments