how to get the thighest bit position in big integers?

Aaron "Castironpi" Brady castironpi at gmail.com
Sun Oct 5 20:04:38 EDT 2008


On Oct 5, 2:12 pm, "Aaron \"Castironpi\" Brady" <castiro... at gmail.com>
wrote:
> Duncan Booth wrote:
> > mmgar... at gmx.de wrote:
>
> > 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]]
>
> OFFSET= tuple( int(x) for x in "5433222211111111" )
> def get_highest_bit_num(r):
>      s = "%x"%r
>      return len(s) * 4 - OFFSET[int(s[0],16)]

That's really counterintuitive.  (That's the word, yes.)  They're both
function calls and both global variables.  Maybe you could use 'len(s)
* 4' to mask out the rest and lookup that index, rather than
converting -back- to integer.  Or, better yet, take 'ord' of s[0].
(Ha ha, -you- time it.)



More information about the Python-list mailing list