Skip to the content
Kets & Gates homeOpen the app

Math toolkit

Bits, XOR and bit strings

x ⊕ y, x · z mod 2, sums over {0,1}ⁿ, and signs that depend on f(x)

about 10 min

Practise this in the app

In the app every question checks your answer and explains a wrong one.

Addition mod 2

XOR, written ⊕\oplus, is addition mod 2: add the bits and keep only whether the total is even (0) or odd (1). So 0⊕0=00\oplus0 = 0, 0⊕1=1⊕0=10\oplus1 = 1\oplus0 = 1 and 1⊕1=01\oplus1 = 0. Three facts do most of the work in these lessons:

  • y⊕0=yy\oplus0 = y and y⊕1=yˉy\oplus1 = \bar y, the flipped bit;
  • y⊕y=0y\oplus y = 0, so XOR-ing the same bit in twice undoes it;
  • order and grouping don’t matter, so a⊕b⊕c{a\oplus b\oplus c} is 1 exactly when an odd number of the bits are 1.

CNOT is XOR made reversible. On the two-qubit basis state ∣a,b⟩\ket{a, b} (first bit aa, second bit bb) it keeps its control and XORs it into the target, ∣a,b⟩→∣a,b⊕a⟩\ket{a, b}\to\ket{a, b\oplus a} (Lesson 6).

Check yourself

Compute 1⊕1⊕1⊕0⊕11\oplus1\oplus1\oplus0\oplus1.

Hint
  1. How many of the bits are 1?
Show the answer

Answer: 00

Four 1s, an even number: (1⊕1)⊕(1⊕1)⊕0=0(1\oplus1)\oplus(1\oplus1)\oplus0 = 0.

Common slips

  • 44: That is the ordinary sum. Mod 2 keeps only whether it is odd or even, and 4 is even, so the answer is 0.
  • 11: Count the 1s: there are four, an even number, so the XOR is 0. Pairing them up helps: 1⊕1=01\oplus1 = 0 twice.

Bit strings and the dot product mod 2

An nn⁠-bit string x=x1x2…xnx = x_1x_2\ldots x_n is one of the 2n2^n elements of {0,1}n\{0,1\}^n, and it also names a basis state of nn qubits, written as the ket ∣x⟩\ket{x}. XOR of two strings works bit by bit: 1011⊕0110=11011011\oplus0110 = 1101. The dot product mod 2 multiplies matching bits and adds the results mod 2:

x⋅z=x1z1⊕x2z2⊕⋯⊕xnzn.x\cdot z = x_1z_1\oplus x_2z_2\oplus\cdots\oplus x_nz_n.

With ordinary sums, as the diagnostic in Lesson 0 writes it, that is

x⋅z=(x1z1+x2z2+⋯+xnzn) mod 2.x\cdot z = (x_1z_1 + x_2z_2 + \cdots + x_nz_n) \bmod 2.

The two agree, because XOR-ing bits together is adding them and keeping only whether the total is odd. Either way, x⋅zx\cdot z is 1 exactly when xx and zz share an odd number of 1s. It appears in the Deutsch–Jozsa algorithm of Lesson 9.

Worked example

1011 · 1110

  1. Line the strings up and multiply bit by bit: 1⋅1=11\cdot1 = 1, 0⋅1=00\cdot1 = 0, 1⋅1=11\cdot1 = 1, 1⋅0=01\cdot0 = 0.

  2. Add mod 2: 1⊕0⊕1⊕0=01\oplus0\oplus1\oplus0 = 0. The strings share two 1s (first and third places), an even number, so 1011⋅1110=0{1011\cdot1110 = 0} and (−1)x⋅z=+1(-1)^{x\cdot z} = +1.

  3. Your turn

    Compute 11011⋅10111{11011\cdot10111}.

    Hint
    1. In how many places do both strings have a 1?
    Show the answer

    Answer: 11

    Matching products: 1,0,0,1,11, 0, 0, 1, 1. Three shared 1s, an odd number, so 11011⋅10111=111011\cdot10111 = 1.

    Common slips

    • 33: That is the ordinary dot product, the number of shared 1s. Mod 2 keeps only whether it is odd: 3 is odd, so the answer is 1.

Check yourself

Using x⋅z=(x1z1+x2z2+⋯+x5z5) mod 2x\cdot z = (x_1z_1 + x_2z_2 + \cdots + x_5z_5) \bmod 2, compute 11011⋅1110111011\cdot11101.

Hints
  1. Multiply the two strings place by place. How many places have a 1 in both?
  2. Add those products as ordinary numbers, then keep only the remainder on division by 2.
  3. The products are 1, 1, 0, 0, 1.
Show the answer

Answer: 11

1⋅1+1⋅1+0⋅1+1⋅0+1⋅1=31\cdot1 + 1\cdot1 + 0\cdot1 + 1\cdot0 + 1\cdot1 = 3, and 3 mod 2=13 \bmod 2 = 1: 3 shared 1s, an odd number.

Line the strings up: 11011 over 11101. The products are 1, 1, 0, 0, 1, so the ordinary sum is 1⋅1+1⋅1+0⋅1+1⋅0+1⋅1=31\cdot1 + 1\cdot1 + 0\cdot1 + 1\cdot0 + 1\cdot1 = 3. Reduce mod 2: 3 mod 2=13 \bmod 2 = 1, so 11011⋅11101=111011\cdot11101 = 1 and the sign (−1)x⋅z(-1)^{x\cdot z} is −1-1.

Common slips

  • 33: 3 is the ordinary sum, the number of places where both strings have a 1. The dot product keeps only whether that is odd: 3 is odd, so x⋅z=1x\cdot z = 1.
  • 1100111001: 11001 is the string of bit-by-bit products (the AND of xx and zz). x⋅zx\cdot z is a single bit: add those products and keep the remainder mod 2, which gives 3 mod 2=13 \bmod 2 = 1.

Sums over all strings, and signs

A sum ∑x∈{0,1}n\sum_{x\in\{0,1\}^n} has one term for each of the 2n2^n strings: for n=2n = 2, the four terms x=00,01,10,11{x = 00, 01, 10, 11}. Deutsch’s algorithm turns a bit f(x)f(x) into a sign, (−1)f(x)(-1)^{f(x)}: +1+1 when f(x)=0f(x) = 0 and −1-1 when f(x)=1f(x) = 1. Signs multiply the way bits add, (−1)a(−1)b=(−1)a⊕b(-1)^a(-1)^b = (-1)^{a\oplus b}, and (−1)k(-1)^k depends only on whether kk is even. One consequence: ∑x(−1)f(x)\sum_x(-1)^{f(x)} is ±2n\pm2^n when ff is constant and 0 when ff is balanced (as many 0s as 1s).

Check yourself

Let f(00)=0f(00) = 0 and f(01)=f(10)=f(11)=1f(01) = f(10) = f(11) = 1. Compute ∑x∈{0,1}2(−1)f(x)\sum_{x\in\{0,1\}^2}(-1)^{f(x)}.

Hint
  1. Write the four signs out, one per string.
Show the answer

Answer: −2-2

(+1)+(−1)+(−1)+(−1)=−2(+1) + (-1) + (-1) + (-1) = -2. This ff is neither constant nor balanced.

Common slips

  • 33: That adds the values f(x)f(x) themselves. Each term is a sign: (−1)0=+1(-1)^0 = +1 for x=00x = 00 and (−1)1=−1(-1)^1 = -1 for the other three.
  • 22: Right size, wrong sign: f(x)=1f(x) = 1 gives −1-1, and three of the four terms have f(x)=1f(x) = 1, so the total is 1−3=−21 - 3 = -2.
  • 00: A total of 0 needs a balanced ff, two 0s and two 1s. This ff has one 0 and three 1s, so the signs do not cancel: 1−3=−21 - 3 = -2.

Check yourself

Compute ∑x∈{0,1}2(−1)x⋅11\sum_{x\in\{0,1\}^2}(-1)^{x\cdot 11}, where x⋅11x\cdot11 is the dot product mod 2.

Hint
  1. Find x⋅11x\cdot11 for each of 00, 01, 10, 11 first.
Show the answer

Answer: 00

The dot products are 0, 1, 1, 0, so the signs are +1,−1,−1,+1+1, -1, -1, +1 and they cancel. For any z≠0…0z \ne 0\ldots0, ∑x(−1)x⋅z=0\sum_x(-1)^{x\cdot z} = 0: half the strings give each sign. That fact is the heart of the Deutsch–Jozsa calculation.

Common slips

  • 44: That counts the terms without their signs. x=01x = 01 and x=10x = 10 each share one 1 with 11, so x⋅11=1x\cdot11 = 1 and those two terms are −1-1.
  • −2-2: x=11x = 11 shares two 1s with 11, so x⋅11=1⊕1=0x\cdot11 = 1\oplus1 = 0 and its sign is +1+1. The four signs are +1,−1,−1,+1+1, -1, -1, +1.

Go further

Free resources for when you want more than this refresher covers.

Lessons that use it

Practise this in the appAll refreshers