Skip to content

Instantly share code, notes, and snippets.

@enif-lee
Created January 1, 2017 13:30
Show Gist options
  • Save enif-lee/24739f080ccb3b87b7eae46cf29d370e to your computer and use it in GitHub Desktop.
Save enif-lee/24739f080ccb3b87b7eae46cf29d370e to your computer and use it in GitHub Desktop.
public class LogUtil {
static int log2 (int number) {
int logCount = 0;
if(65536 <= number) { logCount += 16; number >>= 16; }
if(256 <= number) { logCount += 8; number >>= 8; }
if(16 <= number) { logCount += 4; number >>= 4; }
if(4 <= number) { logCount += 2; number >>= 2; }
return logCount + (2 <= number ? 1 : 0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment