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 -- http://mail.python.org/mailman/listinfo/python-list |
On May 7, 3:13 pm, dmitrey <[hidden email]> 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 -- http://mail.python.org/mailman/listinfo/python-list |
On May 7, 3:31 pm, Mensanator <[hidden email]> wrote:
> On May 7, 3:13 pm, dmitrey <[hidden email]> 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 -- http://mail.python.org/mailman/listinfo/python-list |
In reply to this post by dmitrey.kroshko
dmitrey 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 > -- > http://mail.python.org/mailman/listinfo/python-list > However it's not hard to write. If you really only going to need 8 bits, you can just build a table of bit patterns for each integer. Try this bit of slight cleverness: >>> b = [''] >>> for k in range(8): ... b = [i+j for i in ['0','1'] for j in b] ... >>> len(b) 256 >>> b[63] '00111111' Gary Herron -- http://mail.python.org/mailman/listinfo/python-list |
> Python 3.0 has such a formatting operation, but Python 2.x does not.
> However it's not hard to write. Indeed. Refraining from using named lambdas: >>> def bin(x): ... return ''.join(x & (1 << i) and '1' or '0' for i in ... range(7,-1,-1)) ... >>> bin(12) '00001100' >>> bin(63) '00111111' It would be nice to have str(x, 2) like int(x, 2), but there are bigger fish in pond. Joel -- http://mail.python.org/mailman/listinfo/python-list |
In reply to this post by dmitrey.kroshko
On Wed, 07 May 2008 13:13:40 -0700, dmitrey 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 Here it is: def bin(x, digits=0): oct2bin = ['000','001','010','011','100','101','110','111'] binstring = [oct2bin[int(n)] for n in oct(x)] return ''.join(binstring).lstrip('0').zfill(digits) >>> bin(5) '101' >>> bin(0x0FFF, 16) '0000111111111111' >>> bin(5, 8) '00000101' -- Ivan -- http://mail.python.org/mailman/listinfo/python-list |
In reply to this post by Gary Herron
On May 7, 4:03 pm, Joel Bender <[hidden email]> wrote:
> > Python 3.0 has such a formatting operation, but Python 2.x does not. > > However it's not hard to write. > > Indeed. Refraining from using named lambdas: > > >>> def bin(x): > ... return ''.join(x & (1 << i) and '1' or '0' for i in > ... range(7,-1,-1)) > ... > >>> bin(12) > '00001100' > >>> bin(63) > '00111111' > > It would be nice to have str(x, 2) like int(x, 2), but there are bigger > fish in pond. +1 str( x, base ) +1 -- http://mail.python.org/mailman/listinfo/python-list |
Powered by Nabble | Edit this page |