MegaAlex
MegaAlex

Reputation: 13

Bitwise AND x&y=z, x =?

Just a quick question about the bitwise operator &.

If know that x & y == z and I know the value of y and z is there a way I can calculate the value of x?

Upvotes: 1

Views: 855

Answers (2)

R. Martinho Fernandes
R. Martinho Fernandes

Reputation: 234654

If you are told that x is unique, then no, there is no way of doing it for arbitrary values of y. Otherwise, there are 2n different solutions to that equation, where n is the number of zero bits of y.

As an example, let's consider single bit numbers. If y is one, then the value of x must be the same as z (1&1 = 1; 0&1 = 0). If y is zero, z will also be zero, and x can be either one or zero (0&0 = 0; 1&0 = 0).

On many-bit numbers, every zero bit on y doubles the number of possible values of x, hence 2n

Upvotes: 10

john
john

Reputation: 8027

No, simple example

0&0 = 0
1&0 = 0

Upvotes: 5

Related Questions