0

If I have for example: a = (b << 8) | (c << 4) | d how would I get b c and d from a? Or if I had a and d and needed to get b and c?

2
  • 1
    Do you know something about the range of possible values for b,c,d? Try doing some examples and writing them in binary. Commented Mar 11, 2021 at 23:58
  • Code like that could come up if you were packing 4-bit values in a larger int, in which case e.g. d==a&15 and c==(a>>4)&15 make sense (15 being 1111 binary). But Henry Twist's answer is right that in general "reverse these bitwise operations" doesn't have a unique solution. Commented Mar 12, 2021 at 2:04

1 Answer 1

2

Unfortunately there isn't a single solution for a problem like this (unless in very specific cases). The OR operator (|) doesn't have an inverse, so even the simplified problem of:

a = b | c

where a and c are known, is not solvable (at least to get a single solution).


For a more practical example, you can consider something super simple:

11 = 10 | c

then c could either be 11 or 01 and the number of solutions keep growing with the number of bits.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.