Sequence of 1 and 0 bits of python

In java, to get a sequence of 1s and 0s as a binary number, you can do the following

(-1 << (4))

      

while i give the result

11110000

      

Since python doesn't support signed ints, what's the equivalent way to do it in python? Ideally I would prefer to do this without any external library.

+3


source to share


1 answer


Try:

>>> bin(0xf << 4)[2:]
'11110000'
>>>

      



Or

>>> print ('{0:b}'.format(0xf << 4))
11110000
>>> '{0:b}'.format(0xf << 4)
'11110000'
>>>

      

+2


source







All Articles