Subject description:
To a number N, determine whether it is a power of 2?
Solving direction : according to the characteristics of the binary, a method using a direct outcome of elimination.
Elimination of a law : N - 1 can make the least significant bit of the N 1 to 0, so the least significant bit is a 0 per after 1, so N & (N - 1) (core) can eliminate a minimum of 1.
Problem solution idea: the power of two since the binary representation of only a 1, so the use of an elimination method, the result is necessarily 0.
Code:
// elimination method for solving whether it is a power of two public static Boolean F ( int N) { // N input is not 0 IF ((N & (N- . 1 )) == 0 ) { return to true ; }else{ return false; } }