Skip to content

Instantly share code, notes, and snippets.

@sfaleron
Created September 24, 2015 23:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sfaleron/1f958c6e37f4f235d5c8 to your computer and use it in GitHub Desktop.
Save sfaleron/1f958c6e37f4f235d5c8 to your computer and use it in GitHub Desktop.
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