how to get the thighest bit position in big integers?

Duncan Booth duncan.booth at invalid.invalid
Mon Oct 6 04:20:56 EDT 2008


"Aaron \"Castironpi\" Brady" <castironpi at gmail.com> wrote:

> 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.)

No, the difference between the two is still an extra call to a global 
function.

'ord' may be slightly faster than calling 'int' (function calls with two 
arguments are slower than similar functions with only one), but you have 
still added one extra variable lookup and function call over the original.

-- 
Duncan Booth http://kupuguy.blogspot.com



More information about the Python-list mailing list