How to reverse a bitwise OR operation?

dopatraman picture dopatraman · Nov 4, 2014 · Viewed 11.3k times · Source

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?

Answer

jez picture jez · Nov 4, 2014

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 A, the corresponding bit of B 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).