Topics Numbers and logic

How do computers do math in binary?

Computers do math in binary by representing every number as a string of 0s and 1s, where each digit is a switch that is either off or on. Arithmetic then follows the same carrying rules you learned for decimal addition, only simpler, because each column can hold just 0 or 1. Tiny circuits called logic gates apply those rules to electrical signals, and billions of them working together add, subtract, multiply and divide.

What makes this interesting is how little is going on at the bottom. There is no part of a chip that understands the number seven. There are only gates that follow simple rules about on and off, and a clever arrangement of them called an adder that produces the right answer anyway. Subtraction, negative numbers and decimals all get built on top of that with a few neat tricks, and each trick has a reason behind it.

An episode on this would start with counting in binary, then build up to a working adder, then look at how negatives and fractions get stored, and why the results are sometimes slightly off. On bre, the hosts are AI and can make mistakes, so treat it as a guided walk through the idea rather than a textbook. You can press Talk and ask for a slower pass through any step.

What a bre episode would cover

An outline of the episode bre would make for this question. Every episode is written fresh when you ask, so yours will differ.

  1. Counting with only two digitsHow binary place values work, why each column doubles instead of multiplying by ten, and how to read a binary number.
  2. Switches, signals and logic gatesHow transistors act as switches and combine into AND, OR and NOT gates that follow simple true-or-false rules.
  3. Building an adder from gatesHow a few gates add two bits and carry the extra one, and how chaining them adds whole numbers.
  4. Negative numbers and subtractionWhy most computers use a scheme called two's complement, so that subtraction can reuse the same adding circuit.
  5. Multiplying and dividingHow multiplication becomes repeated shifting and adding, and why division is slower and more involved.
  6. Fractions and rounding quirksHow decimals are stored in floating point, and why a number like 0.1 cannot be held exactly in binary.
  7. Why binary wonWhy two states are far more reliable to build and read than ten, even though they take more digits.

How the episode might open

A sample exchange between two of bre’s AI hosts, bre and Cal. Both are AI; this is written by AI, as every bre episode is.

  1. breAI host

    Here's the picture I want to start with. Inside your phone there is no tiny calculator who knows what seven is. There are just switches, on or off. So how does a pile of switches do your taxes?

  2. CalAI host

    Okay, that's wild when you say it that way. Because I always pictured numbers sitting in there somewhere, like little digits on a screen. But it's just voltage, high or low, and that's the whole alphabet?

  3. breAI host

    That's the whole alphabet. High is a 1, low is a 0. Line up enough of them and you can write any number, the same way ten digits let us write any number in decimal.

  4. CalAI host

    So binary is just decimal with fewer digits. Each column is worth double the one before, instead of ten times. Ones, twos, fours, eights.

  5. breAI host

    Right. So five is a four and a one, which is 101. And here's the nice part: adding in binary is the carrying you did in school, except you carry the moment a column hits two.

  6. CalAI host

    Hold on, how does that actually work? Like, in a circuit. Something has to do the carrying, and it can't be thinking about it.

  7. breAI host

    It isn't thinking. It's a couple of gates arranged so the output lights up exactly when the carry should happen. I don't know a prettier way to say it than that, so let's build one.

  8. CalAI host

    Build it slowly. I'm the guy who once lost an argument to a vending machine.

Questions people also ask

Why do computers use binary instead of decimal?
Two states are much easier to build and read reliably than ten. A transistor only has to be clearly on or clearly off, so small electrical noise rarely causes errors. Decimal hardware is possible, but it would be more complicated and more error-prone for little gain.
How does a computer add two binary numbers?
It uses a circuit called an adder, made of logic gates. For each column it combines the two bits and any carry from the column before, producing a sum bit and a new carry. Chaining these columns together adds numbers of any fixed width.
How do computers handle negative numbers in binary?
Most use a method called two's complement. The leftmost bit carries a negative weight, so a fixed set of bit patterns covers both positive and negative values. Its big advantage is that the same adding circuit can handle subtraction without special cases.
Why does 0.1 plus 0.2 sometimes give a slightly wrong answer?
Just as one third cannot be written exactly in decimal, 0.1 cannot be written exactly in binary. It is stored as a very close approximation. When you add approximations, tiny leftover differences can show up in the last digits of the result.

Related topics

More: all 300 topics, numbers and logic, or the longer reads on /learn.

bre’s hosts are AI, and every episode is generated, so they can be wrong: check anything that matters. This page outlines what an episode would cover. It is for interest and learning, not medical, financial or legal advice.