# When logic starts to count

*The moment gates do math*

Adding two bits is nothing more than an XOR gate for the sum and an AND gate for the carry, which is the moment logic gates stop being abstract truth functions and start performing arithmetic.

Group: Arithmetic
URL: https://digiwleea.wleeaf.dev/learn/gates-to-arithmetic/

> **KEY:** You have built the gates as truth functions: [XOR](https://digiwleea.wleeaf.dev/learn/xor/) outputs `1` when its inputs differ, [AND](https://digiwleea.wleeaf.dev/learn/and/) outputs `1` when both are `1`. They have felt like logic, not math. This short bridge shows that those exact two gates, unchanged, *are* a one-bit adder. Nothing new is built here; you simply notice that arithmetic was already sitting in the gates.

The jump that feels big: logic seems to be about *true and false*, while arithmetic is about *numbers*. How does a gate that answers a yes/no question suddenly add? The answer is that adding two single bits produces a two-bit result, and each of those two output bits is itself a simple yes/no question about the inputs, exactly the kind of question a gate already answers.

Add two bits `A` and `B`. The possible sums are `0`, `1`, or `2`, which in binary are `00`, `01`, `10`. So the result has a **sum** bit (the ones place) and a **carry** bit (the twos place). Now ask the two yes/no questions:

1. **When is the sum bit `1`?** Exactly when one input is `1` and the other is `0`, that is, when they **differ**. That is XOR: `sum = A XOR B`.
2. **When is the carry bit `1`?** Exactly when both inputs are `1` (only `1 + 1` reaches two). That is AND: `carry = A AND B`.

| A | B | carry | sum |
| --- | --- | --- | --- |
| 0 | 0 | 0 | 0 |
| 0 | 1 | 0 | 1 |
| 1 | 0 | 0 | 1 |
| 1 | 1 | 1 | 0 |

_The carry column is exactly AND; the sum column is exactly XOR. Two gates you already have compute the full one-bit addition._

Look at the two output columns. The carry column is `0, 0, 0, 1`, the AND truth table. The sum column is `0, 1, 1, 0`, the XOR truth table. So `1 + 1 = 10` is not a special new operation, it is an AND gate saying "carry" and an XOR gate saying "sum is zero". Logic was doing arithmetic all along; you just had not read the columns as numbers yet.

> **WARN:** Do not reach for OR when you mean XOR. OR outputs `1` on the `1 + 1` row too, which would make the sum bit wrong (it should be `0` there, with the `1` going to the carry). The sum bit is XOR precisely because it must go *back* to `0` once the column overflows into a carry.

**Q (Try it):** Using only the rule "sum = A XOR B, carry = A AND B", compute the two-bit result of `1 + 1`, then of `1 + 0`.

**A:** For `1 + 1`: sum = `1 XOR 1` = `0`, carry = `1 AND 1` = `1`, giving `carry sum` = `10`, which is two. Correct. For `1 + 0`: sum = `1 XOR 0` = `1`, carry = `1 AND 0` = `0`, giving `01`, which is one. Two gates reproduce single-bit addition exactly.

### FAQ

**Q:** How do you add two bits with logic gates?

**A:** Use one XOR gate for the sum bit and one AND gate for the carry bit. `sum = A XOR B` and `carry = A AND B`. Those two gates together compute the complete addition of two single bits.

**Q:** Why is the sum bit XOR and not OR?

**A:** Because when both inputs are `1` the sum bit must be `0` (the result `2` is `10` in binary, with the `1` going to the carry). XOR is `0` on that row; OR is wrongly `1`. XOR is the function that returns to `0` once a column overflows.

**Q:** When do logic gates become arithmetic?

**A:** As soon as you read their output columns as the bits of a number. Adding two bits is XOR for the sum and AND for the carry, so the same gates that answer true/false questions also add.

> **KEY:** Those two gates side by side are the [half adder](https://digiwleea.wleeaf.dev/learn/halfadder/). Next you will give it a carry-in and chain copies to add whole multi-bit numbers.
