Last active
August 29, 2015 14:22
-
-
Save st98/05f9f2d9c81a4ebe64c3 to your computer and use it in GitHub Desktop.
int.bit_length()
This file contains 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
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 |
This file contains 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
#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