Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 660
dopatraman Avatar asked Nov 04 '14 03:11

dopatraman


People also ask

Can you reverse Bitwise operations?

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.

What is the reverse of Bitwise or?

Only reverse operation possible is for XOR as it is non-destructive.

How do you negate a number Bitwise?

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.


2 Answers

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).

like image 86
jez Avatar answered Sep 20 '22 22:09

jez


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.

like image 40
Aseem Yadav Avatar answered Sep 19 '22 22:09

Aseem Yadav