Flip bits twice with XOR

Choose which bits to flip. Then use the same choices to put the byte back the way it was.

Follow the worked example below to see which positions flip and how the same mask restores the original bits.

One pair of bits at a time

In Lesson 3.1, you wrote a byte as eight bits. With XOR, you line up two rows of bits and apply the same rule to every column. A mask bit of 0 keeps the input because 0 XOR 0 is 0 and 1 XOR 0 is 1. A mask bit of 1 flips the input because 0 XOR 1 is 1 and 1 XOR 1 is 0.

Applying the same mask twice
Position12345678
Input10110010
Mask00110101
Pass 110000111
Pass 210110010

Why the same mask undoes the change

The first application changes 10110010 into 10000111. The second takes that result as its input. Where the mask is 0, the bit stays the same both times. The mask has a 1 at positions 3, 4, 6, and 8. Those bits flip once and then flip back, restoring 10110010.

The one-time pad will use a secret mask as the key. The next few lessons ask where the bits for that mask can come from.

Check your understanding

In the worked example, change mask position 2 from 0 to 1. Which bit of the first result changes compared with the table? What happens when you apply your new mask again?

Show the answer

Only position 2 changes in the first result. The input bit at position 2 is 0, and the new mask bit flips it to 1. Apply the same new mask again and that 1 flips back, restoring the original byte.