Skip to content

Instantly share code, notes, and snippets.

@st98
Last active August 29, 2015 14:22
Show Gist options
  • Save st98/05f9f2d9c81a4ebe64c3 to your computer and use it in GitHub Desktop.
Save st98/05f9f2d9c81a4ebe64c3 to your computer and use it in GitHub Desktop.
int.bit_length()
n = int(1e6)
def f(x):
l = 0
while x != 0:
l += 1
x >>= 1
return l
f(n) # 20
import math
math.floor(math.log2(n)) + 1 # 20
len(bin(n)[2:]) # 20
n.bit_length() # 20
#include <limits.h>
#include <stdio.h>
unsigned int f(unsigned int x) {
return CHAR_BIT * sizeof(int) - __builtin_clz(x);
}
int main(void) {
printf("[+] %d\n", f(1e6));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment