Lite / Textbooks / Digital Logic and Computer Architecture / Chapter 4
4. Binary arithmetic
Introduction
Hardware adds the way you were taught at school, one column at a time with a carry, but in base 2. This chapter writes long binary numbers compactly in hexadecimal, builds a ripple-carry adder from full adders, and introduces two's complement, the code that lets the same adder handle negative numbers. It ends with the cases where the answer no longer fits.
Figure 4.1. Three-bit two's complement on a wheel (described in words)
Eight patterns arranged on a circle: 000 is 0, 001 is 1, 010 is 2, 011 is 3, 100 is −4, 101 is −3, 110 is −2 and 111 is −1. Stepping clockwise adds one; the jump from 011 to 100 is where signed overflow happens.
Explanation
Built from 3 School lessons. Each part is the lesson's own explanation.
4.1 Binary and hexadecimal
A sensor datasheet says its ID register reads 0x6B. What number is that, and why would anyone write it that way?
Place values that double
In binary each place is worth twice the one to its right: 1, 2, 4, 8, 16, 32, 64, 128. 1011 = 8 + 0 + 2 + 1 = 11. To go the other way, take away the biggest power of 2 that fits and repeat: 13 = 8 + 4 + 1 = 1101.
How much fits
n bits make 2ⁿ patterns, holding 0 to 2ⁿ − 1. 4 bits: 0 to 15. 8 bits (a byte): 0 to 255. 16 bits: 0 to 65,535. Each extra bit doubles the range.
Hex: four bits per digit
Hexadecimal has 16 digits: 0–9 then A = 10, B = 11, C = 12, D = 13, E = 14, F = 15. Each hex digit is exactly four bits, so a byte is two hex digits: 0x6B = 0110 1011 = 6 × 16 + 11 = 107. 0xFF = 255. The '0x' just marks a number as hex.
Common mix-ups
8 bits can hold numbers up to 256.
✓ 8 bits make 256 patterns, but they start at 0, so the largest is 255.Hex is a different kind of number.
✓ It is the same number written in base 16; only the notation changes.
Open the Lite lesson: Binary and hexadecimal
4.2 Multi-bit addition and the ripple-carry adder
Adding 0111 + 0001 changes every single bit of the answer. How long does a circuit have to wait before it can trust the result?
By hand: right to left
Line the numbers up, start at the rightmost column and add the two bits plus any carry. Write the sum bit and carry the rest. 1011 + 0110: 1 + 0 = 1; 1 + 1 = 10 → 0 carry 1; 0 + 1 + 1 = 10 → 0 carry 1; 1 + 0 + 1 = 10 → 0 carry 1. Result 1 0001 = 17 = 11 + 6.
The ripple-carry adder
One full adder per column, each carry out wired to the next carry in. It is simple and small, but a column cannot be sure of its answer until the carry from the right has arrived, so the carry ripples across the whole adder.
Worst-case delay
Each gate takes a little time to settle. In the virtual computer's model the first column's carry is ready after 3 gate delays (XOR, AND, OR), and each further column adds 2. A 4-bit adder needs 3 + 2 × 3 = 9 delays; 32 bits would need 65. Faster designs, such as carry-lookahead, compute carries in parallel at the cost of more gates.
Common mix-ups
All the bits of a sum appear at the same instant.
✓ In a ripple-carry adder the higher bits wait for carries from the lower ones.
Open the Lite lesson: Multi-bit addition and the ripple-carry adder
4.3 Overflow and negative numbers
In a 4-bit computer, 7 + 1 = −8. That is not a bug in the adder. So what is going on?
Unsigned overflow
k bits hold 0 to 2ᵏ − 1. If a sum is bigger, the extra bit leaves as carry out and the register keeps the sum minus 2ᵏ. 4-bit 1011 + 0110 = 11 + 6 = 17 → keeps 1 (0001), carry out 1. The carry out flag is how hardware reports unsigned overflow.
Two's complement
To store negatives, let the top bit be worth −2ᵏ⁻¹. In 4 bits the range becomes −8 to 7: 0111 = 7, 1000 = −8, 1111 = −1. To negate, invert every bit and add 1. The same adder circuit then adds signed numbers correctly, so subtraction is just adding a negative.
Signed overflow
If two numbers of the same sign give a result of the opposite sign, the true answer did not fit. 0111 + 0001 = 1000: 7 + 1 'became' −8. Adding a positive and a negative can never overflow.
Real consequences
In 1996 the first Ariane 5 rocket was lost less than a minute after launch when its guidance software converted a 64-bit floating-point value into a 16-bit signed integer and the value did not fit. Embedded engineers choose widths with headroom and test the edges.
Derivation: Why 'invert and add 1' gives −n
In k bits, n plus its bitwise inverse ¬n is all 1s: for example 0101 + 1010 = 1111. So n + ¬n = 2ᵏ − 1.
Add 1 to both sides: n + (¬n + 1) = 2ᵏ.
A k-bit register cannot hold 2ᵏ: it wraps to 0. So in k-bit arithmetic n + (¬n + 1) = 0, which means ¬n + 1 behaves exactly like −n.
Example, 4 bits: 5 = 0101, ¬5 = 1010, +1 = 1011. Check: 0101 + 1011 = 1 0000, which wraps to 0000. ✓
Equivalently, the top bit is worth −2ᵏ⁻¹ instead of +2ᵏ⁻¹: 1011 = −8 + 2 + 1 = −5.
Common mix-ups
A negative number just has a minus-sign bit in front of the normal value.
✓ That is sign-magnitude, which needs special adder logic. Two's complement lets the ordinary adder work for negatives too.Overflow means the computer crashes.
✓ Usually it silently wraps, which is worse: the program carries on with a wrong number.
Open the Lite lesson: Overflow and negative numbers
Worked examples
Binary and hexadecimal: example 1
Convert 0xC8 to binary and decimal.
Solution
C = 12 = 1100 and 8 = 1000, so 1100 1000. Decimal: 12 × 16 + 8 = 200.
Multi-bit addition and the ripple-carry adder: example 1
Add 0101 + 0011 by hand, showing carries.
Solution
1 + 1 = 10 → 0 carry 1. 0 + 1 + 1 = 10 → 0 carry 1. 1 + 0 + 1 = 10 → 0 carry 1. 0 + 0 + 1 = 1. Result 1000 = 8 = 5 + 3.
Overflow and negative numbers: example 1
Write −3 in 4-bit two's complement and check by adding 3.
Solution
3 = 0011 → invert 1100 → add 1: 1101. Check: 0011 + 1101 = 1 0000 → 0000 in 4 bits. ✓
Interactive figures, in words
Figure 4.2 from Overflow and negative numbers
Set 4 bits. Add 1111 + 0001 and read the unsigned result and carry out. Then add 0111 + 0001 and read the signed view: why is 7 + 1 shown as −8? Find a pair of negative numbers whose sum looks positive. Switch to 8 bits and repeat one of your examples.
The adder stepper shows each number's unsigned and signed values. In 4 bits, 1111 + 0001 gives 0000 with a carry out: unsigned overflow. 0111 + 0001 gives 1000, which is 8 unsigned but −8 signed: signed overflow, because two positive numbers gave a negative result. In 8 bits the signed range is −128 to 127.
Laboratory
These run in a browser on the full site (they need a faster connection). Simulations are simplified educational models.
- Silicon Studio: Multi-bit arithmetic (Silicon Studio stage)
Add 0111 and 0001 as unsigned and as signed numbers, and explain why only one reading overflows.
Open on the full site - Simulation OS: Adder architecture (Simulation OS experiment)
Compare ripple-carry and carry-lookahead delay as the width grows, and note what the faster design costs.
Open on the full site - BUILD: Virtual computer, Level 3 (BUILD level)
Trace a carry through the build's adder from bit 0 to the top bit.
Open on the full site
Practice
Each question links to its Lite lesson, where your answer is checked on our server and a wrong answer gets a hint. Answers are never sent to your device.
- What is binary 1011 in decimal?
Answer it in “Binary and hexadecimal” - What is 0x2A in decimal?
Answer it in “Binary and hexadecimal” - Add 0110 + 0111 (4-bit). Give the result as a decimal number.
Answer it in “Multi-bit addition and the ripple-carry adder” - Using the model 'first carry after 3 gate delays, then 2 more per extra bit', how many gate delays does an 8-bit ripple-carry adder need for its final carry?
Answer it in “Multi-bit addition and the ripple-carry adder” - In a 4-bit unsigned register, what is 1011 + 0110? Give the value the register holds, in decimal.
Answer it in “Overflow and negative numbers” - What is −5 in 4-bit two's complement? Give the bit pattern's unsigned value in decimal.
Answer it in “Overflow and negative numbers” - Which 4-bit two's complement addition overflows?
Answer it in “Overflow and negative numbers”
Engineering applications
Reading memory dumps
Engineers read registers and memory in hexadecimal because each hex digit is exactly four bits: 0x2A is 0010 1010, which is 42.
Sensor arithmetic
A signed 16-bit reading spans −32768 to 32767. Adding two large positive readings can produce a negative result, so firmware checks for overflow or uses a wider type.
Watch (optional)
Streaming video uses a lot of data. Everything you need is in the text.
- “Learn how computers add numbers and build a 4 bit adder circuit” by Ben Eater
- “Representing Numbers and Letters with Binary: Crash Course Computer Science #4” by CrashCourse
Summary
- Hexadecimal is shorthand for binary: one hex digit stands for four bits.
- A ripple-carry adder chains full adders; its worst-case delay grows with the number of bits because the carry passes through every stage.
- In n-bit two's complement the top bit has weight −2^(n−1), so the range is −2^(n−1) to 2^(n−1) − 1.
- To negate in two's complement, invert every bit and add one.
- Signed overflow happens when two numbers of the same sign give a result of the opposite sign.
Glossary
- Hexadecimal
- Hexadecimal is base 16, using digits 0–9 and A–F. Each hex digit stands for exactly four bits, so two hex digits describe one byte. Field Guide
- Two's complement (signed integers)
- Two's complement represents negative integers so that ordinary binary addition still works: the top bit has a negative place value, so an n-bit number ranges from −2^(n−1) to 2^(n−1) − 1. Field Guide
- Full adder and ripple-carry adder
- A full adder adds three bits (A, B and a carry in) to give a sum and a carry out. Chaining n of them, each carry out feeding the next carry in, makes an n-bit ripple-carry adder. Field Guide
- Propagation delay
- Propagation delay is the time a gate's output takes to respond to a change at its input. The slowest path between flip-flops, the critical path, sets the fastest safe clock. Field Guide
Project: A safe 8-bit adder specification
Specify an 8-bit adder for signed sensor values that raises a flag on overflow. Give the flag's rule in terms of the two input sign bits and the result's sign bit, and test it on at least six cases, including both edges of the range.
Deliverables
- The overflow rule as a Boolean expression
- Six or more test cases with expected results
- A note on what the firmware should do when the flag is raised
Write it up on paper or in your own notes. Lite never stores what you write.
References
- David Money Harris and Sarah L. Harris (2012). Digital Design and Computer Architecture, 2nd edition. Morgan Kaufmann.
- David A. Patterson and John L. Hennessy (2017). Computer Organization and Design RISC-V Edition: The Hardware Software Interface. Morgan Kaufmann.