pythonbit-manipulationbitwise-operatorsbitwise-or

How to reverse a bitwise OR operation?


Here's what I've done:

93 | 199

which returns

223

I understand that this is because 0b1011101 | 0b11000111 is 0b11011111

However, suppose I want to do the reverse operation. How do I get 0b1011101 from a bitwise operation between 0b11000111 and 0b11011111?


Solution

  • You can't get an unambiguous answer in the general case. If C=A|B, then wherever you have a 1 in C and a 1 in B, the corresponding bit of A could have been either 0 or 1.

    In your example, 93|199=223, but 92|199 is also 223. So, given 223 and 199 there's no single answer (in fact, in this example there are 32 possible answers).