howto print binary number

castironpi at gmail.com castironpi at gmail.com
Wed May 7 16:40:31 EDT 2008


On May 7, 3:31 pm, Mensanator <mensana... at aol.com> wrote:
> On May 7, 3:13 pm, dmitrey <dmitrey.kros... at scipy.org> wrote:
>
> > hi all,
> > could you inform how to print binary number?
> > I.e. something like
>
> > print '%b' % my_number
>
> > it would be nice would it print exactly 8 binary digits (0-1, with
> > possible start from 0)
>
> > Thank you in advance, D
>
> The gmpy works good.
>
> >>> import gmpy
> >>> print gmpy.digits(66,2).zfill(8)
>
> 01000010

>>> print '\n'.join( [ ''.join( [ str( a>> i & 1 ) for i in range( 7, -1, -1 ) ]
 ) for a in range( 10 ) ] )
00000000
00000001
00000010
00000011
00000100
00000101
00000110
00000111
00001000
00001001



More information about the Python-list mailing list