What does bitwise XOR (exclusive OR) mean?

前端 未结 8 1307
眼角桃花
眼角桃花 2020-12-02 05:52

I\'m trying to understand the binary operators in C# or in general, in particular ^ - exclusive or.

For example:

Given an array of positive intege

8条回答
  •  日久生厌
    2020-12-02 06:44

    To see how it works, first you need to write both operands in binary, because bitwise operations work on individual bits.

    Then you can apply the truth table for your particular operator. It acts on each pair of bits having the same position in the two operands (the same place value). So the leftmost bit (MSB) of A is combined with the MSB of B to produce the MSB of the result.

    Example: 2^10:

        0010 2
    XOR 1010 8 + 2
        ----
        1    xor(0, 1)
         0   xor(0, 0)
          0  xor(1, 1)
           0 xor(0, 0)
        ----
     =  1000 8
    

    And the result is 8.

提交回复
热议问题