python binary number code example
Example 1: python int binary
print('{0:b}'.format(3))
print('{0:8b}'.format(3))
print('{0:08b}'.format(3))
def int2bin(integer, digits):
if integer >= 0:
return bin(integer)[2:].zfill(digits)
else:
return bin(2**digits + integer)[2:]
print(int2bin(3, 6))
Example 2: python binary
num = 10
bin(num)
Example 3: binary representation python
>>> bin(88)
'0b1011000'
>>> int('0b1011000', 2)
88
>>>
>>> a=int('01100000', 2)
>>> b=int('00100110', 2)
>>> bin(a & b)
'0b100000'
>>> bin(a | b)
'0b1100110'
>>> bin(a ^ b)
'0b1000110'
Example 4: python binary representation of numbers
int(bin(8)[2:])