binary representation of an integer

cokofreedom at gmail.com cokofreedom at gmail.com
Tue Jun 24 07:57:54 EDT 2008


On Jun 24, 10:38 am, Mark Dickinson <dicki... at gmail.com> wrote:
> 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

Strange in 2.6, but I know at least in 3.0 that all integers are C
Long's now, so the L is no longer required.



More information about the Python-list mailing list