There is a solution available for doing bitwise operations. But, it converts the integers in binary number and then do bitwise operations. I already have the numbers in binary format. How do I carry out the bitwise operations for these numbers? Ex. x = 10101, y = 11001 I want z = x or y to be 11101. Any help is highly appreciated.
1 Answer
You can convert decimal 10101 to binary 10101 by converting it to string then int(.., 2) (2 => base 2):
>>> str(x)
'10101'
>>> int(str(x), 2)
21
Do operation you want (| => bitwise or):
>>> int(str(x), 2) | int(str(y), 2)
29
Then, convert it back to decimal, using format (or str.format with format string b -> meaning binary representation) and int:
>>> format(int(str(x), 2) | int(str(y), 2), 'b')
'11101'
>>> int(format(int(str(x), 2) | int(str(y), 2), 'b')) # back to integer
11101