Created
April 21, 2016 11:46
-
-
Save acmi/8647ce7bbe9c5fbc9358d5b87f14b121 to your computer and use it in GitHub Desktop.
log2 java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static int log2(int bits) { | |
int log = 0; | |
if (bits >= 0x10000) { | |
bits >>>= 16; | |
log = 16; | |
} | |
if (bits >= 0x100) { | |
bits >>>= 8; | |
log += 8; | |
} | |
if (bits >= 0x10) { | |
bits >>>= 4; | |
log += 4; | |
} | |
if (bits >= 4) { | |
bits >>>= 2; | |
return log + (bits >>> 1); | |
log += 2; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment