Binary Addition and Overflow
Add binary numbers column by column with carries, check results in decimal, and understand overflow when a sum doesn't fit in the available bits.
Welcome back!
Step 1 of 13Learn
Four rules for adding bits
You add binary numbers the same way you learned to add decimal numbers in elementary school: line them up on the right, add column by column from right to left, and carry when a column overflows. The difference is that binary overflows much sooner, because 2 is already "10."
There are only four cases to learn:
| Bits added | Sum | Write | Carry |
|---|---|---|---|
| 0 + 0 | 0 | 0 | 0 |
| 0 + 1 | 1 | 1 | 0 |
| 1 + 1 | 2 = 10 in binary | 0 | 1 |
| 1 + 1 + 1 (with a carry) | 3 = 11 in binary | 1 | 1 |
Example: add 0110 (6) and 0111 (7).
carries: 1 1
0 1 1 0
+ 0 1 1 1
---------
1 1 0 1- Ones column: 0 + 1 = 1. Write 1, no carry.
- Twos column: 1 + 1 = 10. Write 0, carry 1.
- Fours column: 1 + 1 + carried 1 = 11. Write 1, carry 1.
- Eights column: 0 + 0 + carried 1 = 1. Write 1.
Result: 1101, which is 13. Check in decimal: 6 + 7 = 13. Converting both inputs and the answer to decimal is the best way to catch mistakes, especially a forgotten carry.
Step 2 of 13Practice · Fill in the blanks
Fill in the blanks
In binary, 1 + 1 = , so you write 0 and carry 1. And 1 + 1 + 1 = , so you write 1 and carry 1.
Step 3 of 13Flashcards
The four rules of binary addition
Tap a card to flip it.
Step 4 of 13Practice · Work it out
Add the binary numbers 1011 and 0110. Enter the result in binary digits, such as 1001.
Step 5 of 13Learn
When the answer doesn't fit
In the last example, two 4-bit numbers produced a 5-bit answer. On paper, you just write the extra digit. But computers store numbers in a fixed number of bits. If a calculation needs more bits than are available, the extra carry has nowhere to go. This is called overflow.
Suppose a value is stored in a single unsigned byte (8 bits, values 0 to 255). Add 200 and 100:
11001000 (200)
+ 01100100 (100)
----------
100101100 (300 needs 9 bits)The ninth bit is dropped, leaving 00101100, which is 44. The computer reports 44 instead of 300, because the result wrapped around: 300 − 256 = 44.
Think of a car's odometer that only has six digits. After 999,999 miles it rolls over to 000,000. A binary counter does the same thing at 2n.
Overflow has caused real problems. Some old video games had counters that rolled over, turning a huge score into a tiny one or glitching a level. Programmers prevent overflow by choosing big enough data types (32 or 64 bits instead of 8), checking values before calculating or using languages that switch automatically to larger integers.
Step 6 of 13Practice · Diagram
An 8-bit counter wraps around
Tap the value an unsigned 8-bit counter shows right after 255.
Tap the shape that answers the question, then press Check.
- 253
- 254
- 255
11111111: every bit is 1.
- 0
Adding 1 needs a ninth bit, which is lost, so the byte shows 00000000.
- 1
- 2
Step 7 of 13Practice · Work it out
An unsigned 8-bit counter holds 250. What value does it show after 10 is added, if the overflow wraps around?
Step 8 of 13Try the calculator
8-bit unsigned addition
Enter two numbers from 0 to 255 to see the true sum, what an 8-bit unsigned byte would store, and whether overflow happened (1 means yes).
- True sum
- 300
- Stored in 8 bits
- 44
- Overflow (1 = yes)
- 1
Step 9 of 13Learn
Shifting and multiplying
Addition is the foundation of computer arithmetic. Subtraction, multiplication and division can all be built from it, along with one more simple operation: shifting.
A left shift moves every bit one place to the left and puts a 0 at the right end. Since every place value doubles, the number doubles:
00000101 (5) shifted left → 00001010 (10) → 00010100 (20)
A right shift moves every bit one place to the right, dropping the rightmost bit. That halves the number, discarding any remainder:
00010101 (21) shifted right → 00001010 (10)
Shifting is extremely fast in hardware, so compilers often replace "multiply by 8" with "shift left by 3." Many programming languages expose it directly with the operators << and >>: in JavaScript, 5 << 1 is 10 and 21 >> 1 is 10.
Shifting can overflow too. Shift 10000000 (128) left in an 8-bit value and the 1 falls off the end, leaving 0. Multiplication in general is built from shifts and adds: to multiply by 10, a computer can add (x shifted left 3) and (x shifted left 1), since 8x + 2x = 10x.
Step 10 of 13Practice · Chart
Shifting 00000011 to the left
Tap the result after shifting left two places.
Tap the bar that answers the question, then press Check.
Step 11 of 13Note
Step 12 of 13Practice · Code challenge
Write addBinary(a, b) that adds two binary strings of the same length and returns the binary result as a string, keeping any extra carry bit. For example, addBinary("0110", "0111") returns "1101" and addBinary("1011", "0110") returns "10001". Work column by column from the right, without converting to decimal.
addBinary("0110", "0111")addBinary("1011", "0110")addBinary("0001", "0001")addBinary("1111", "0001")
Step 13 of 13Practice · Choose one
In an 8-bit system, what happens when 10000000 (128) is shifted left by one place?
Lesson complete!