30% off your first 3 months

Get the deal

Binary numbers and why computers use them

by DuckyHelperNov 12, 20264 min read
Computer science

TL;DR:

  • Binary uses two digits, 0 and 1. Each place is worth twice the place to its right: 1, 2, 4, 8, 16 and so on.

  • To convert to binary, divide by 2 repeatedly and read the remainders from last to first. To convert back, add the place values that have a 1.

  • A bit is one binary digit. A byte is 8 bits and can hold 256 different values, 0 to 255.

You write numbers using ten digits, 0 to 9, and each place is worth ten times the one on its right. A computer uses two digits, and each place is worth two times the one on its right. Everything else about binary follows from that one change.

The idea in plain words

In decimal, the number 305 means 3 × 100 + 0 × 10 + 5 × 1. The places are powers of ten: 1, 10, 100. In binary, the places are powers of two: 1, 2, 4, 8, 16, 32 and so on, and each digit, called a bit, is either 0 or 1.

Place value

16

8

4

2

1

Example bits

0

1

1

0

1

The bits 01101 mean 8 + 4 + 1 = 13.

Why computers use it

A computer is built from switches, called transistors, that are either on or off. Two states are easy to make and easy to tell apart, even when the electrical signal is a bit noisy. Telling ten voltage levels apart reliably is much harder. So the hardware stores a 1 as one state (say high voltage) and a 0 as the other.

Any number, letter, picture or sound can be stored as a pattern of bits, as long as everyone agrees on the code. For example, the capital letter A is stored as the number 65, which in 8 bits is 01000001, because 64 + 1 = 65.

Worked example: 13 in binary is 1101

Method 1: repeated division by 2. Divide by 2, write the remainder, and continue with the quotient until you reach 0.

  1. 13 ÷ 2 = 6 remainder 1

  2. 6 ÷ 2 = 3 remainder 0

  3. 3 ÷ 2 = 1 remainder 1

  4. 1 ÷ 2 = 0 remainder 1

Read the remainders from the last one to the first: 1 1 0 1. So 13 in binary is 1101.

Method 2: place values. Find the largest power of two that fits, subtract it, and repeat. The largest power of two not above 13 is 8, and 13 − 8 = 5. The next is 4, and 5 − 4 = 1. 2 does not fit in 1, so that bit is 0. 1 fits. The bits for 8, 4, 2, 1 are 1, 1, 0, 1, again 1101.

Converting back. Take 10110. The places from the right are 1, 2, 4, 8, 16, so the bits give 1×16 + 0×8 + 1×4 + 1×2 + 0×1 = 16 + 4 + 2 = 22. Check it by converting 22 forwards: 22 ÷ 2 = 11 r 0, 11 ÷ 2 = 5 r 1, 5 ÷ 2 = 2 r 1, 2 ÷ 2 = 1 r 0, 1 ÷ 2 = 0 r 1. Reading upward gives 10110. It matches.

Bits, bytes and how many values fit

With 1 bit you have 2 values. With 2 bits, 4 (00, 01, 10, 11). Every extra bit doubles the count, so n bits give 2ⁿ values. A byte is 8 bits, which gives 2⁸ = 256 values, written as the numbers 0 to 255. The biggest byte, 11111111, is 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255.

You can check any of this in Python: bin(13) returns '0b1101', and int('1101', 2) returns 13.

Adding in binary

Binary addition works like decimal addition, except you carry when a column reaches 2, not 10. Add 13 and 3. In binary that is 1101 + 0011. Working from the right: 1 + 1 = 10, so write 0 and carry 1. Next column: 0 + 1 + 1(carry) = 10, write 0, carry 1. Next: 1 + 0 + 1 = 10, write 0, carry 1. Last: 1 + 0 + 1 = 10, write 0, carry 1, which becomes a new leftmost bit. The answer is 10000, which is 16, and 13 + 3 = 16.

This is what the hardware in a processor does, using simple on-off circuits. Everything bigger, from multiplication to video, is built from steps this small.

How to check your answer

  • Convert your answer back to decimal. If you get your original number, it is right.

  • The number of bits must be enough: 13 needs 4 bits because it is more than 7 (the largest 3-bit value) and at most 15.

  • The leftmost bit of a result from division should be the last remainder.

  • A binary number never contains a digit above 1.

Common mistakes

  • Reading the remainders in the wrong order, which gives 1011 (which is 11), not 1101.

  • Adding place values from the wrong end. The smallest place, worth 1, is on the right.

  • Saying 2ⁿ is the largest number n bits can store. It is the count of values. The largest value is 2ⁿ − 1.

  • Confusing bits and bytes. A byte is eight bits.

Where you will use it next

Binary shows up in file sizes, colours, network addresses and logic. Combining bits with rules is the subject of boolean logic and truth tables, and the precise step-by-step thinking behind conversions is the idea in what an algorithm is.

Frequently asked questions

Why not use decimal in a computer?

It could be done, but the hardware would be far more complex and less reliable. Two states are simple and robust.

Is hexadecimal a different thing?

It is a shorthand for binary. Each hexadecimal digit stands for exactly four bits.

Get started

DuckyHelper can
do more

It sees your screen and works in any app you use. Try it for 10 minutes, no card.

Get it for Mac, Windows or Linux

Share

Questions and jobs

Do anything

Ask it a question or hand it a job, in any app.

Doing jobs for you is in early access on Mac. Asking works on Mac, Windows and Linux.