Checking if a number is power of 2 or not This is my solution in Java and I need your opinion:

public class Main {

  /**
   * 
   */

  public static void main(String[] args) {

    Scanner in = new Scanner(System.in);
    int number = in.nextInt();
    in.close();

    if (isPowerOfTwo(number)) {
      System.out.println("yes");
    } else {
      System.out.printf("no");
    }
  }

  private static boolean isPowerOfTwo(int number) {

    if (number % 2 != 0) {
      return false;
    } else {

      for (int i = 0; i &lt;= number; i++) {

        if (Math.pow(2, i) == number) return true;
      }
    }
    return false;
  }
}
There's actually a bit hack for this :

private static boolean isPowerOfTwo(int number) {

    return number &gt; 0 &amp;&amp; ((number &amp; (number - 1)) == 0);

}


(ref : Bit Twiddling hacks)

This exploits the fact, that in binary notation a power of two is a 1 followed by a number of 0's, and the number just below is all 1's equal to that number of 0's :

  100000000 // number
&amp; 011111111 // number - 1
-----------
  000000000