binary representation of an integer
Mark Dickinson
dickinsm at gmail.com
Tue Jun 24 04:38:53 EDT 2008
On Jun 24, 9:03 am, eliben <eli... at gmail.com> wrote:
> What would be the quickest way to do this ? I think that for dec2bin
> conversion, using hex() and then looping with a hex->bin lookup table
> would be probably much faster than the general baseconvert from the
> recipe.
I suspect you're right, but it would be easy to find out: just
code up the hex->bin method and use the timeit module to do some
timings. Don't forget to strip the trailing 'L' from hex(n) if n
is a long.
If you're prepared to wait for Python 2.6, or work with the beta
version, then the conversion is already there:
Macintosh-3:trunk dickinsm$ ./python.exe
Python 2.6b1+ (trunk:64489, Jun 23 2008, 21:10:40)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> bin(13)
'0b1101'
Interestingly, unlike hex and oct, bin doesn't add a trailing
'L' for longs:
>>> bin(13L)
'0b1101'
I wonder whether this is a bug...
Mark
More information about the Python-list
mailing list