Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the ^ (XOR) operator do? [duplicate]

Tags:

operators

math

What mathematical operation does XOR perform?

like image 349
Hilary Park Avatar asked Jan 25 '13 16:01

Hilary Park


People also ask

How does XOR find duplicate numbers?

Step 1 : Find “min” and “max” value in the given array. It will take O(n). Step 2 : Find XOR of all integers from range “min” to “max” ( inclusive ). Step 3 : Find XOR of all elements of the given array.

What does the XOR operator do?

XOR is a bitwise operator, and it stands for "exclusive or." It performs logical operation. If input bits are the same, then the output will be false(0) else true(1).

What happens when you XOR the same number?

XOR on the same argument: x ^ x = 0 If the two arguments are the same, the result is always 0 .

How do I find duplicate numbers?

Find the Duplicate Number - LeetCode. Given an array of integers nums containing n + 1 integers where each integer is in the range [1, n] inclusive. There is only one repeated number in nums , return this repeated number. You must solve the problem without modifying the array nums and uses only constant extra space.


1 Answers

XOR is a binary operation, it stands for "exclusive or", that is to say the resulting bit evaluates to one if only exactly one of the bits is set.

This is its function table:

a | b | a ^ b --|---|------ 0 | 0 | 0 0 | 1 | 1 1 | 0 | 1 1 | 1 | 0 

This operation is performed between every two corresponding bits of a number.

Example: 7 ^ 10
In binary: 0111 ^ 1010

  0111 ^ 1010 ======   1101 = 13 

Properties: The operation is commutative, associative and self-inverse.

It is also the same as addition modulo 2.

like image 138
phant0m Avatar answered Sep 17 '22 05:09

phant0m