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
?
You can't do that because you have thrown away information (i.e. bits) - you can't get information back from nowhere. Note that both AND ( & ) and OR ( | ) are destructive. The only Boolean operations that are reversible are XOR ( ^ ) and NOT ( ~ ). Save this answer.
Only reverse operation possible is for XOR as it is non-destructive.
Negative numbers are usually represented using two's complement instead of sign-magnitude, where you take the bitwise NOT of the positive version of the number and add one to it. So negative numbers with a small magnitude get represented by a bunch of 1's. The sum-of-2^x scheme only applies to positive numbers.
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).
As pointed out here, both OR and AND are destructive operations. Reversing OR operation is a lossy operation and as mentioned by 'jez' can have more than one answer. So, it is not possible
Only reverse operation possible is for XOR as it is non-destructive.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With