Skip to content

Instantly share code, notes, and snippets.

@acmi
Created April 21, 2016 11:46
Show Gist options
  • Save acmi/8647ce7bbe9c5fbc9358d5b87f14b121 to your computer and use it in GitHub Desktop.
Save acmi/8647ce7bbe9c5fbc9358d5b87f14b121 to your computer and use it in GitHub Desktop.
log2 java
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