What does bitwise XOR (exclusive OR) mean?

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

    As it is obvious from the name(bitwise), it operates between bits. Let's see how it works, for example, we have two numbers a=3 and b=4, the binary representation of 3 is 011 and of 4 is 100, so basically xor of the same bits is 0 and for opposite bits, it is 1. In the given example 3^4, where "^" is a xor symbol, will give us 111 whose decimal value will be 7. for another example, if you've given an array in which every element occurs twice except one element & you've to find that element. How can you do that? simple xor of the same numbers will always be 0 and the number which occur exactly once will be your output. because the output of any one number with 0 will be the same name number because the number will have set bits which zero don't have.

提交回复
热议问题