This is a nifty little one-liner I wrote which takes a power of two (an integer x where x = 2n) and returns the log2x.
class Pow2 {
public static void main(String[] args) {
int i = 1;
int n = 1048576;
while( ((n >> ++i) % 2) == 0 );
System.out.println("Power of 2: " + i);
}
}
- None Found