binary values in python code example
Example 1: binary operation python
x << y
"left shifted x by y places"
x >> y
"right shift x by y places"
x & y
"bitwise and"
x | y
"bitwise or".
~ x
"Complement of x"
x ^ y
"bitwise exclusive or"
Example 2: 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