Base 2 to long integers and back
Fredrik Lundh
fredrik at pythonware.com
Wed Mar 21 02:40:49 EST 2001
Tim Churches wrote:
> but there is no built-in or library function which does the
> reverse, it would seem. However your code does the trick
> nicely and quite quickly
for larger numbers, something like this is 8-10 times
faster:
binmap = []
for i in range(256):
binmap.append(
"%d%d%d%d%d%d%d%d" % (
(i&0x80)!=0, (i&0x40)!=0, (i&0x20)!=0, (i&0x10)!=0,
(i&0x08)!=0, (i&0x04)!=0, (i&0x02)!=0, (i&0x01)!=0
)
)
def f2(val, joiner="".join, binmap=binmap):
out = []
append = out.append
while 1:
append(binmap[val & 255])
val >>= 8
if not val:
break
out.reverse()
return joiner(out)
Cheers /F
<!-- (the eff-bot guide to) the standard python library:
http://www.pythonware.com/people/fredrik/librarybook.htm
-->
More information about the Python-list
mailing list