Skip to content

Instantly share code, notes, and snippets.



Created Sep 24, 2015
What would you like to do?
Quick ln2 function for Python
from math import ceil
# for example, inc=4 gives ln16, or the number of hexadecimal digits
# required to represent n.
def ln2(n, inc=1):
if n<0:
raise ValueError('math domain error')
i = 0
n = int(ceil(n))
while n:
i += 1
n >>= inc
return i
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment