to binary in python code example
Example 1: how to convert decimal to binary python
a = 5
print(bin(a))
Example 2: 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 3: decimal to binary in python
a = 10
bnr = bin(a).replace('0b','')
x = bnr[::-1]
while len(x) < 8:
x += '0'
bnr = x[::-1]
print(bnr)
Example 4: how to get the binary value in python
bin(19)
def binary(num):
array = []
while(num > 0):
sol1 = num / 2
check = sol1 - int(sol1)
if(check > 0):
array.append("1")
num = sol1 - 0.5
else:
array.append("0")
num = sol1
while(len(array) < 4):
array.append("0")
array = array[::-1]
string = ''.join(array)
return string