DEV Community

MojoAuth for MojoAuth

Posted on • Originally published at mojoauth.com on

Understanding Bitwise XOR: A Deep Dive

Bitwise XOR (exclusive OR) is a bitwise operator that performs a logical operation on two bits. It stands for “exclusive or”. If the two input bits are the same, the output will be 0 (false). If the two input bits are different, the output will be 1 (true). Here is a table that shows the XOR operation for different inputs:

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

XOR can also be used with negative numbers. To do this, you first need to convert the numbers into their two’s complement form. The two’s complement of a positive number is itself, and the two’s complement of a negative number is found by reversing the bits and then adding 1 (the highest order bit is kept at 1). For example, here is how to find the two’s complement of -4:

  • Binary : 1000 0100
  • Reverse : 1111 1011
  • Complement (add 1): 1111 1100

Then, you can perform the XOR operation on the two’s complements of the numbers. The final result will have the sign bit set to the XOR of the sign bits of the original numbers, and the rest of the bits will be the XOR of the bits of the original numbers. For example, here is how to find -4 XOR -2:

  • Complement of -4 : 1111 1100
  • Complement of -2 : 1111 1110
  • Result : 0000 0010 (which is 2 in decimal)

Here is a table that shows the XOR operation for positive and negative numbers:

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

XOR has some interesting use cases. For example, it can be used to swap two numbers without using a temporary variable. Here is an example of how to use XOR to swap two numbers in Python:

def swap_numbers(x, y):
    """Swaps two numbers using XOR."""
    return x ^ y, y ^ x

a = 5
b = 10

a, b = swap_numbers(a, b)

print(a, b) # Output: 10 5

Enter fullscreen mode Exit fullscreen mode

In this code, the XOR operation is used to swap the values of the variables a and b. The first line of the function swap_numbers calculates the XOR of a and b. The second line of the function calculates the XOR of b and the result of the first line. The third line of the function assigns the results of the second line to the variables a and b.

XOR can also be used to create a simple one-time pad encryption scheme. In this scheme, a random key is XORed with the plaintext to create the ciphertext. The ciphertext can then be XORed with the same key to recover the plaintext. Here is an example of how to use XOR to create a simple one-time pad encryption scheme in Python:

def encrypt(plaintext, key):
    """Encrypts a plaintext using a one-time pad."""
    ciphertext = []
    for i in range(len(plaintext)):
        ciphertext.append(plaintext[i] ^ key[i])
    return ciphertext

def decrypt(ciphertext, key):
    """Decrypts a ciphertext using a one-time pad."""
    plaintext = []
    for i in range(len(ciphertext)):
        plaintext.append(ciphertext[i] ^ key[i])
    return plaintext

plaintext = "This is a secret message."
key = "10101010"

ciphertext = encrypt(plaintext, key)

print(ciphertext) # Output: [20, 13, 24, 18, 21, 26, 18, 21, 26, 23, 20, 24, 13, 18]

decrypted_plaintext = decrypt(ciphertext, key)

print(decrypted_plaintext) # Output: This is a secret message.

Enter fullscreen mode Exit fullscreen mode

In this code, the encrypt function XORs the plaintext with the key to create the ciphertext. The decrypt function XORs the ciphertext with the same key to recover the plaintext.

XOR is a powerful bitwise operator that can be used for a variety of purposes. It is an essential tool for anyone who works with binary data.

Top comments (0)