Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequence of 1's and 0's python bits

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

(-1 << (4))

while will give me the result

11110000

Since python does not support signed ints, what is the equivalent way of doing this in python? Ideally, I would prefer to do this without any external library.

like image 804
random_coder_101 Avatar asked Sep 24 '26 08:09

random_coder_101


1 Answers

Try :

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

Or,

>>> print ('{0:b}'.format(0xf << 4))
11110000
>>> '{0:b}'.format(0xf << 4)
'11110000'
>>>
like image 120
Md. Rezwanul Haque Avatar answered Sep 26 '26 21:09

Md. Rezwanul Haque