how to get the thighest bit position in big integers?

Duncan Booth duncan.booth at invalid.invalid
Sun Oct 5 12:48:29 EDT 2008


mmgarvey at gmx.de wrote:

> My question to the group: Does anyone know of a non-hackish way to
> determine the required bit position in python? I know that my two
> ideas
> can be combined to get something working. But is there a *better* way,
> that isn't that hackish?
> 
How about using the hex representation of the value?

OFFSET = dict(("%x"%i, int(c)) for i,c in enumerate("5433222211111111"))
def get_highest_bit_num(r):
	s = "%x"%r
	return len(s) * 4 - OFFSET[s[0]]



More information about the Python-list mailing list