Join us on the demo, while our product experts provide a detailed walkthrough of our enterprise platform.

logoAsync

The latest news in the world of engineering.

Identity and Access Management (IAM), including security and customer experience.

Visit Blog

Grow your business to millions.Engage and retain your customers.

Visit Blog

How does bitwise ^ (XOR) work?

Everything you should know about bitwise ^ xor operator.

Kheenvraj Lomror
Kheenvraj Lomror
November 26, 2020
2 min read

Free, Secure and Trusted Way to Authenticate Your Visitors

Add login to your website in 5 minutes completely for free!

Free Sign UpNo hidden costs. No credit card needed.

How does bitwise ^ (XOR) work?

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).

XOR table:

X Y X^Y
0 0 0
0 1 1
1 0 1
1 1 0

Example: 4^3 = 7

In binary: 
	     1000
	   ^ 0011
	    ------
 Result:     1011  => (7)

XOR with negative numbers

Let's understand with an example -4^-2 = 2 In the above example, we can see -4^-2output will be 2 but the question arises how? Because if we represent both inputs in the binary form, then we do XOR of bits, then the output will be 0000 0110 and in decimal, it will be 6 but as we know output should be 2.

  1000 0100 (-4)
^ 1000 0010 (-2)
------------------
  0000 0110 => (6) // incorrect output

Note: Here, the leftmost bit position is reserved for the sign of the value (positive or negative) and doesn't contribute towards the value of the number.

Let's understand how the XOR operation works with negative numbers.

How XOR operation works with negative numbers?

First, the XOR operation is to XOR each bit (the same is 0, the difference is 1), but you need to convert the number into a complement first.

  1. The complement of a positive number is itself
  2. The complement of the negative number is reversed for each bit and then incremented by 1 (the highest is kept at 1)

    // Lets take -4
    In binary: 			1000 0100
    Reverse: 			1111 1011
    complement (Increment by 1): 	1111 1100

// Now -2 In binary: 1000 0010 Reverse: 1111 1101 complement (Increment by 1): 1111 1110

Final result:

complement of -4 : 1111 1100
complement of -2 : 1111 1110
                  -----------
Result:            0000 0010 	=> 2

Here, the MSB bit of result will denote the sign, and the rest of the bits will denote the value of the final result. XOR sign table could be useful to understand the sign of result:

X Y X^Y
+ + +
+ - -
- + -
- - +

The above approach will work with negative inputs, but if we have positive and negative, then?

How XOR operation works with positive and negative numbers?

Let's performs -5 ^ 2 Follow the same above approach to get a complement code of -5.

complement of -5 :          1111 1011
// Note: complement of a positive number is itself
complement of 2 :           0000 0010  
complement result:          1111 1001

Now, complement result -1 : 1111 1000
                           -----------
Final result(inverse code): 1000 0111   => -7

Some interesting use cases of XOR operator

  • Swap two number without the third variable Example:

    function swapWithXOR (a, b) {
        a = a^b;
        b = a^b;
        a=a^b;
        console.log("result => a: " + a + ", b: " + b)
    }
    
    swapWithXOR(4, 1); // result => a: 1, b: 4
  • RAID Drive Backups (Systems)
  • Cryptography (XOR cipher)
  • Array operations

LoginRadius Docs

Implement Authentication in Minutes

click here

Most Popular Tags

EngineeringJavaScriptReactAuthenticationNodeJsCSSSecurityC#JWTOAuth

Do you want a free authentication solution?

Add the world's most secure, reliable and easy to implement user authentication solution on your applications at $0Get Started Free

Kheenvraj Lomror

Kheenvraj Lomror

Software Developer @LoginRadius. JavaScript Lover, love to work with Angular, React

View Profile

Try a Modern Authentication Solution

$0/ month

Free Sign Up
  • 7,000 Monthly Active Users
  • 1 Web or Mobile App
  • Email/Password Login
  • 3 Social Login Providers
  • User Data Storage
  • Transactional Email Template
  • Customizable Login Interfaces