First I am not sure what is going on in this bitwise operation.
I get code written and supply to other parties as code snippets.
Now if VAR is unsigned 8bit integer (unsigned char) and r is either 0 or 1 or 2 or 4.
Can following be reversed if the value of r is known and resulting value is there.
VAR |= 1 << r; //that is 200 where VAR was 192 and r was 3
For example initial value of VAR is 192 and value of r is 3 *result is 200*.
Now if I have this 200, and I know the value of r that was 3, can I reverse it back to 192 ?
I hope it is most easy one, but I don’t know these bitwise operations, so forgive me.
Thanks
The answer is no. This is because the
|(OR) operator is not a one-to-one function.In other words, there are multiple values of
VARthat can produce the same result.For example:
If you tried to invert it, you wouldn’t be able to tell whether the original value is
0or8.A similar situation applies to the
&AND operator.From an information-theory perspective:
The operators
|and&incur a loss of information and do not preserve the entropy of the data.On the other hand, operators such as
^(XOR),+, and-are one-to-one and thus preserve entropy and are invertible.