how to get the thighest bit position in big integers?

Rich Healey healey.rich at gmail.com
Mon Oct 6 02:02:30 CEST 2008


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Aaron "Castironpi" Brady wrote:
> Duncan Booth wrote:
>> 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]]
>> -- 
>> http://mail.python.org/mailman/listinfo/python-list
>>
> 
> You can replace the dict if it's faster.
> 
> 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)]
> 
> P.S.  Back home, this sort of 'nitpicking' would be judged
> unconstructive.  Worth pointing out, or not worth saying?
> 
> P.S.S.  'Thighest' bit?  I thought the spam filters would catch that.
> 
That should be P.P.S.

PS: This is also unconstructive nitpicking.

Kind regards


Rich ;)

- --
Rich Healey - iTReign      \    .''`.   /         healey.rich at gmail.com
Developer / Systems Admin   \  : :' :  /        healey.rich at itreign.com
AIM: richohealey33           \ `. `'  /             richo at psych0tik.net
MSN: bitchohealey at hotmail.com \  `-  / richohealey at hellboundhackers.org
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkjpVZUACgkQLeTfO4yBSAcgeACgr45Hu4XKyMjCf0jwq1LR35tU
Lv8AnRn2RgHCxJ3QwYpNO8/DjLncKv2t
=/WZa
-----END PGP SIGNATURE-----



More information about the Python-list mailing list