Binary Numbers Explained With Conversions and Arithmetic
Convert between binary and decimal, then add, subtract, multiply and divide in base two, with worked examples you can check.
Key takeaways
- Binary place values are powers of two, and a number's decimal value is the sum of the places that hold a 1.
- To convert decimal to binary, divide by two repeatedly and read the remainders from last to first.
- Binary addition needs only a few facts, the key one being that 1 + 1 = 10.
- Many simple decimal fractions, including 0.1, repeat forever in binary, which explains some surprising computer output.
Binary is base two. It writes every number with just two digits, 0 and 1, and each position is worth twice the position to its right: ones, twos, fours, eights and so on. So the binary numeral 1011 means 8 + 2 + 1, which is eleven.
Everything you know about decimal arithmetic carries over. The only change is that you regroup in twos instead of tens. Once you have the place values, conversions and calculations are mechanical.
Place values
Here are the first several places. A small subscript tells you the base, so 101₂ is binary and 101₁₀ is decimal.
| Power | 2⁷ | 2⁶ | 2⁵ | 2⁴ | 2³ | 2² | 2¹ | 2⁰ |
|---|---|---|---|---|---|---|---|---|
| Value | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
A single binary digit is called a bit. A group of 8 bits is commonly called a byte.
Counting goes 0, 1, 10, 11, 100, 101, 110, 111, 1000. Whenever a position runs out of digits, which happens after 1, it resets to 0 and the position to its left goes up. It is an odometer with only two symbols on each wheel. If positional notation is new to you, start with how place value works.
Binary to decimal
Method 1: add the places. For 101101₂, write the values above the digits and add those with a 1.
digit: 1 0 1 1 0 1
value: 32 16 8 4 2 1
sum: 32 + 8 + 4 + 1 = 45 Method 2: double and add. Start at the left with 0. For each digit, double your running total and add the digit.
digit 1: 2×0 + 1 = 1
digit 0: 2×1 + 0 = 2
digit 1: 2×2 + 1 = 5
digit 1: 2×5 + 1 = 11
digit 0: 2×11 + 0 = 22
digit 1: 2×22 + 1 = 45 The second method is fast for mental work on long strings, since you never need to know the large powers of two.
Reading patterns at a glance
A few shapes are worth recognizing, since they let you read many numbers without adding up every place.
| Pattern | Value | Why |
|---|---|---|
| 1000₂ | 8 | a single 1 is a power of two |
| 1111₂ | 15 | all ones is one less than the next power |
| 1010₂ | 10 | 8 + 2 |
| 10000001₂ | 129 | 128 + 1 |
The second row generalizes: a string of n ones equals 2ⁿ − 1. You can see why by adding 1 to it, which sets off a chain of carries and produces a 1 followed by n zeros.
The last bit tells you whether the number is odd. The last two bits give the remainder on division by 4, and the last three the remainder on division by 8. In decimal, the last digit plays the same role for division by 10.
Decimal to binary
Repeated division produces the bits from right to left. Convert 45.
45 ÷ 2 = 22 remainder 1
22 ÷ 2 = 11 remainder 0
11 ÷ 2 = 5 remainder 1
5 ÷ 2 = 2 remainder 1
2 ÷ 2 = 1 remainder 0
1 ÷ 2 = 0 remainder 1
read remainders from bottom to top: 101101 The first remainder tells you whether the number is odd or even, which is the ones bit. Dividing by 2 then shifts everything down one place, exposing the next bit.
Subtracting powers works from the left. For 200: take 128, leaving 72. Take 64, leaving 8. Skip 32 and 16. Take 8, leaving 0. Put 1 in the places used and 0 elsewhere: 11001000₂.
Addition
You need these facts:
0 + 0 = 0
0 + 1 = 1
1 + 1 = 10 write 0, carry 1
1 + 1 + 1 = 11 write 1, carry 1 Add 1011₂ (eleven) and 110₂ (six).
1011
+ 110
------
10001 Working from the right: 1 + 0 = 1. Then 1 + 1 gives 0, carry 1. Then 0 + 1 + 1 gives 0, carry 1. Then 1 + 1 gives 10. The result 10001₂ is 16 + 1 = 17, as expected.
Subtraction
When you borrow in binary, the 1 you take from the next position is worth two in the current one. Compute 10010₂ − 111₂, which is 18 − 7.
10010
− 111
------
1011 The answer 1011₂ is 11. The quickest check is to add back: 1011₂ + 111₂ = 10010₂.
Computers usually avoid a separate borrowing process by using two's complement, a way of encoding negative numbers. With a fixed width of 8 bits, you find −5 as follows.
5 = 00000101
flip bits = 11111010
add 1 = 11111011 this pattern represents −5 Adding 5 to it gives 100000000, a 9-bit result. The leading bit falls outside the 8-bit width and is discarded, leaving 0. In 8-bit two's complement the representable range is −128 to 127. Results outside the range overflow.
Multiplication and division
The multiplication table is tiny: anything times 0 is 0, and 1 × 1 = 1. So long multiplication is just shifting and adding. Compute 101₂ × 11₂, or 5 × 3.
101
× 11
-----
101 101 × 1
+ 1010 101 × 1, shifted one place
-----
1111 = 15 Appending a 0 to a binary whole number doubles it, just as appending a 0 in decimal multiplies by ten. Removing the last bit halves it and discards any remainder.
Division is long division with only one question at each step: does the divisor fit, yes or no? For 1111₂ ÷ 101₂ the quotient is 11₂, which matches 15 ÷ 5 = 3.
A longer worked example
Put the operations together by computing 13 × 11 entirely in binary. In binary, 13 is 1101 and 11 is 1011.
1101
× 1011
-------
1101 for the ones bit
11010 for the twos bit
000000 for the fours bit (it is 0)
+ 1101000 for the eights bit
-------
10001111 To total the rows safely, add them two at a time:
1101 + 11010 = 100111 (13 + 26 = 39)
100111 + 1101000 = 10001111 (39 + 104 = 143) Check the final value: 128 + 8 + 4 + 2 + 1 = 143, which is 13 × 11.
Adding two rows at a time avoids columns with large carries. Hardware multipliers rest on the same idea of shifted copies added together, though real circuits use faster arrangements.
Notice the middle row of zeros. You can skip it when writing by hand, as long as the next row is shifted by the correct number of places.
Fractions in binary
Places to the right of the point are halves, quarters, eighths and so on. So 0.101₂ = 1/2 + 1/8 = 0.625.
To convert a decimal fraction, multiply by 2 repeatedly and record the whole-number part each time.
0.1 × 2 = 0.2 → 0
0.2 × 2 = 0.4 → 0
0.4 × 2 = 0.8 → 0
0.8 × 2 = 1.6 → 1
0.6 × 2 = 1.2 → 1
0.2 × 2 = 0.4 → 0 (the pattern now repeats)
0.1 = 0.000110011001100...₂ A fraction in lowest terms terminates in binary only when its denominator is a power of two. Since 10 includes the factor 5, one tenth never ends. A computer that stores it in binary floating point keeps a rounded value, which is why 0.1 + 0.2 can print oddly.
How big can n bits get?
With n bits there are 2ⁿ different patterns. For unsigned whole numbers they run from 0 to 2ⁿ − 1.
| Bits | Patterns | Largest unsigned value |
|---|---|---|
| 4 | 16 | 15 |
| 8 | 256 | 255 |
| 10 | 1,024 | 1,023 |
| 16 | 65,536 | 65,535 |
Each added bit doubles the count. In the other direction, the number of yes-or-no questions needed to pin down one item among N, by halving the possibilities each time, is the smallest n with 2ⁿ ≥ N. About 20 questions suffice for a million items, since 2²⁰ = 1,048,576.
Why machines use base two
An electronic switch is easy to build and read when it has two clearly separated states. Telling apart ten voltage levels reliably would be harder, since small amounts of noise could turn one level into the next.
Two-valued logic also matches binary arithmetic neatly. The sum bit of two inputs is 1 when exactly one of them is 1, and the carry bit is 1 when both are. Circuits built from Boolean operations therefore add numbers.
The idea is older than electronics. Gottfried Wilhelm Leibniz published an account of binary arithmetic in 1703. Because long strings of bits are tiring to read, programmers often group them in fours and write hexadecimal: 11001000₂ splits into 1100 and 1000, giving C8, which is 12 × 16 + 8 = 200.
Encoding more than numbers
Bits can stand for anything once there is an agreed table. In the ASCII character code, the capital letter A is assigned the number 65, which is 01000001 in 8 bits. Lowercase a is 97, or 01100001. The two differ in a single bit, the one worth 32.
Unicode extends the idea to the characters of the world's writing systems, and encodings such as UTF-8 specify how those code numbers become bytes.
Pictures and sound work similarly. A color on a screen is often stored as three numbers for red, green and blue, each from 0 to 255. A digital recording is a long series of numbers measuring the signal at regular instants.
In every case the bits themselves carry no label. The same byte might be a number, a letter or a shade of blue, and the software has to know which. When a file opened in the wrong program shows nonsense, the bits are intact and the interpretation is wrong.
A related point concerns units. Memory sizes are sometimes counted in powers of two, with 1 KiB meaning 1,024 bytes, while 1 kB in the metric sense means 1,000 bytes. Check which is intended when the difference matters.
Comparing and rounding in binary
Comparing two unsigned binary numbers works as it does in decimal. After removing leading zeros, the longer numeral is the larger. If the lengths match, scan from the left, and the first position where they differ decides: 11010₂ is larger than 10111₂ because its second digit is 1 where the other has 0.
The reason is that one place is worth more than all the places to its right put together. The place worth 8 beats 4 + 2 + 1 = 7.
That fact also explains how coarse or fine a binary approximation is. If you keep k places after the point and drop the rest, the error is less than 1/2ᵏ. Keeping 10 places gives an error below 1/1,024, a little better than three decimal places. Each extra bit halves the possible error, so a bit buys less precision than a decimal digit does. Roughly speaking, ten bits match three decimal digits, because 2¹⁰ = 1,024 is close to 10³ = 1,000.
Practice and common mistakes
Try converting 77 to binary, then convert your answer back. You should get 1001101₂, since 64 + 8 + 4 + 1 = 77.
Watch for these slips:
- Reading remainders from top to bottom, which reverses the number.
- Reading 10₂ as "ten." It is two, and it helps to say "one zero."
- Forgetting the 0 bits in unused positions.
- Mixing bases in one calculation without converting.
- Treating a pattern such as 11111011 as having one fixed meaning. It is 251 as an unsigned value and −5 in 8-bit two's complement, so you must know the encoding.