Tanishq Soni
Tanishq Soni

Reputation: 11

Can anyone simplify this bitwise expression

(a&b)^(a|b) how can I simply this, if you can add any reference for learning simplification it will be better

Upvotes: 1

Views: 1091

Answers (1)

user555045
user555045

Reputation: 64904

(a&b)^(a|b) == a^b

So it gets pretty simple.

Why? Let's look at just one bit, because all the bits are independent. The truth table for the original expression would be:

a b a&b a|b  ^
0 0  0   0   0
0 1  0   1   1
1 0  0   1   1
1 1  1   1   0

Which is the same truth table as just a ^ b would have.

An other way to look at it, which is also visible in the truth table, is that for XOR we can swap the left and right operand as we wish. And we can do that for each bit independantly. So say we start with a ^ b, then we could, if we wanted, arrange the operands so that as many as possible zeroes are swapped into the left operand, and as many as possible ones are swapped into the right operand. For 00 and 11 that changes nothing, but we change 10 into 01. How could we do that? By taking a & b as the left operand, and a | b as the right operand, and then it's back to your original expression.

Upvotes: 2

Related Questions