What does bitwise XOR (exclusive OR) mean?

前端 未结 8 1308
眼角桃花
眼角桃花 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:29

    The bitwise operators treat the bits inside an integer value as a tiny array of bits. Each of those bits is like a tiny bool value. When you use the bitwise exclusive or operator, one interpretation of what the operator does is:

    • for each bit in the first value, toggle the bit if the corresponding bit in the second value is set

    The net effect is that a single bit starts out false and if the total number of "toggles" is even, it will still be false at the end. If the total number of "toggles" is odd, it will be true at the end.

    Just think "tiny array of boolean values" and it will start to make sense.

提交回复
热议问题