BusXOR

Inputs

PinTypeDescription
Abus8First input bus (8 bits)
Bbus8Second input bus (8 bits)

Outputs

PinTypeDescription
Qbus8Bitwise XOR: A ^ B

How It Works

BusXOR is a difference detector. It compares two buses bit by bit and puts a 1 wherever the bits disagree: 0 against 1, or 1 against 0. Matches give zero. The truth table has a will of its own: 0 XOR 0 = 0, 0 XOR 1 = 1, 1 XOR 0 = 1, but 1 XOR 1 = 0. Where AND and OR answer "are there any ones here", XOR answers "is there a difference here".

Two key properties follow from "difference gives one". First: A XOR A = 00000000 — comparing a byte with itself always yields zero. Second: XOR with the mask 11111111 flips every bit: 10101010 XOR 11111111 = 01010101, that is X XOR 11111111 = NOT X. You can narrow the mask: XOR with 00001111 inverts only the low nibble, leaving the high one untouched.

A subtlety: XOR is its own inverse. Encrypt a byte with a key K and you get C = A XOR K. Apply the same key again and A comes back intact: C XOR K = A. Neither AND nor OR can do this, and that is exactly why XOR underlies the simplest ciphers and checksums.

8-bit Examples

A (bin)B (bin)Q (bin)Q (dec)
10101010111111110101010185
1100110011001100000000000
000011111111000011111111255
101101100000111110111001185

Usage

In the ALU (level 13), XOR is part of the basic four operations. But real fame comes to it in tandem with the zero detector: run A XOR B through BusZero and the processor gets a ready-made comparator — a 1 on the output means the two bytes are equal. This is how the compare command and all the "if equal" conditional jumps are built.

In the computer project (level 16 and beyond), XOR toggles bits: apply the mask 00000001 to a register — the low bit blinks; apply it again — it goes dark. Simple encryption works on the same property: a value is XORed with a key and stored "shaken up", and reading it back with a second XOR of the same key restores the original. Checksums are built on XOR too: XORing all bytes of a message in a row instantly reveals whether even a single bit was lost on the way.

Build it yourself →

Related components

Related articles

Frequently Asked Questions

What is bitwise XOR for?

Comparing, encryption and toggling bits. A number XORed with itself always gives 0.

How do I compare two bytes for equality?

Feed them into A and B, and route the result into BusZero. If the bytes are equal, XOR gives 00000000 and the output is 1. Any difference in at least one bit gives 0.

Why is XOR called a bit toggle?

Ones in the mask flip the selected bits, zeros leave them in place. Apply the mask twice and the bits return: 1 XOR 1 = 0. Perfect for toggle operations like blinking an LED.