1

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 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.