PentaHex

XOR

The XOR gate, or Exclusive OR gate, is a logical operation. It's best thought about in binary (only 0 and 1).

The XOR has the following properties:

The XOR is the equivalent of adding the numbers in base 2, modulus 2: (1 + 1) mod 2 = 0, (1 + 0) mod 2 = 1

The XOR logic is either one, but not both. It's an exclusive OR, because it excludes the true case of true OR true, which is ambiguous.

In a chain of XORs, such as 1 XOR 0 XOR 1 XOR 1, an odd number of the inputs need to be true. For example, 1 XOR 0 XOR 1 XOR 1 = 1 because 3 inputs are 1 (odd). 1 XOR 0 XOR 1 XOR 0 = 0 because 2 inputs are 1 (even).

Although the XOR is a bitwise operation for binary numbers, it can be done on numbers in different bases by converting to binary and on strings by writing them by character codes in binary.

Example 1

What is 11011012 XOR 10010101012?

  1. First, align the bits to be the same length (by prepending 0s): 00011011012 XOR 10010101012
  2. Then, go bit by bit (digit by digit) and do the XOR operation: 10001110002

An XOR in binary is super simple! Let's try it on decimal numbers.

Example 2

What is 32 XOR 23?

The first part of this problem is converting 32 and 23 from base 10 to base 2. We're going to do this quickly, but if you want to learn more about converting between bases and number bases in general, see this tutorial

  1. Converting 32 to base 2 is simple, since it is 2^5. It's the fifth power, so there are 5 zeroes: 100000. If doesn't make sense, recall that the third power of 10 (1000) has 3 zeroes in base 10.
  2. Convert 23 to base 2 is dividing it by 2 repeatedly.
    23 / 2 = 11 R 1
    11 / 2 = 5 R 1
    5 / 2 = 2 R 1
    2 / 2 = 1 R 0
    1 / 2 = 0 R 1
    Our converted number is 101112.

Now that we have two base 2 numbers, getting the XOR is as simple as the last problem.

Notice that doing an XOR with a 0 from any digit is that digit, so the last 5 zeroes make this XOR very easy. 100000 XOR 10111 = 110111

Example 3

If the XOR of 'a' and '-' is 'L', what's the XOR of 'L' and 'a'?

To solve this problem, we would have to convert the characters to their corresponding character codes, then convert that number to binary. Then, we would take the XOR, convert the binary to decimal, and then convert the character code to the ASCII character.

That's... a lot of work. Thankfully, the XOR has a unique property. If a XOR b = c, then a XOR c = b, and b XOR c = a.

In this example, we can see that the XOR would just be '-', without having to do any work.

Example 4

Let's do that last example again, but through that long process of actually calculating the XOR.

  1. 'L' has a character code of 76. 'a' has a character code of 97.
  2. To convert 76 to base 2, divide by 2 repeatedly:
    76 / 2 = 38 R 0
    38 / 2 = 19 R 0
    19 / 2 = 9 R 1
    9 / 2 = 4 R 1
    4 / 2 = 2 R 0
    2 / 2 = 1 R 0
    1 / 2 = 0 R 1
    The final converted number is 10011002.
  3. To convert 97 to base 2, notice that it is 1 more than 96. 96 is an easy number in binary, 32 + 64, so 11000002. Adding 1 is 11000012.
  4. The binary XOR is the easiest part: 1001100 XOR 1100001 = 0101101
  5. 1011012 = 1 × 32 + 1 × 8 + 1 ×4 + 1 = 45.
  6. The ASCII character with character code 45 is, as expected, '-'.

XOR Tools

The XOR is a common CTF topic. That's why we've created two XOR tools to help you out. The XOR Calculator quite simply allows you to take the XOR of numbers (binary, decimal, hex) or text.

The XOR Finder allows you to brute force the XOR on text, and filter it to find a specific string and just ASCII.