Search code examples
xor

Exclusive logical sum (XOR)


I have this question :

xor

The answer is D, because they explain :

(n XOR 1111 1111) will reverse 0 to 1 and 1 to 0 , then plus the result with 1, we have two's complement

But as i understand, the XOR operation is :

a logical operation that outputs true whenever both inputs differ

example :

1011 XOR 1000 = 0011

So how can (n XOR 1111 1111) is inverted n (one's complement) ??


Solution

  • Because you are using all 1s in the second operand,

    • If 1st operand has 1, then you get 1 XOR 1 = 0 (same inputs so false)

    • If 1st operand has 0, then you get 0 XOR 1 = 1 (different inputs so true)

    So for the 1st operand, 1 becomes 0, and 0 becomes 1. Thus the number is bit-reversed.

    So in your example,

    1011 XOR 1111 = 0100