Skip to content
Tactiverge

Textbooks/Digital Logic and Computer Architecture/Chapter 4

4Binary arithmetic

Version 1 · Technically checked (automated checks only) · 3 source lessons · 7 practice items

Text
All chapters and search
  1. 1. Information as bits
  2. 2. Logic gates and Boolean algebra
  3. 3. Combinational building blocks
  4. 4. Binary arithmetic
  5. 5. From silicon to switches
  6. 6. State machines and sequential logic
  7. 7. Instructions and the datapath
  8. 8. Performance and pipelining
  9. 9. The memory hierarchy
  10. 10. Power, performance and the chip

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.

0000001101020113100-4101-3110-2111-1clockwise = +1dashed: 3 + 1wraps to −4(signed overflow)top bit weight −4
Figure 4.1. Three-bit two's complement on a wheel. 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

This chapter is built from 3 School lessons. Each part below is the lesson's own explanation; open the lesson for its predict-and-discover activities.

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 full 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 full 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
  1. In k bits, n plus its bitwise inverse ¬n is all 1s: for example 0101 + 1010 = 1111. So n + ¬n = 2ᵏ − 1.

  2. Add 1 to both sides: n + (¬n + 1) = 2ᵏ.

  3. 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.

  4. Example, 4 bits: 5 = 0101, ¬5 = 1010, +1 = 1011. Check: 0101 + 1011 = 1 0000, which wraps to 0000. ✓

  5. 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 full 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

Figure 4.2 from Overflow and negative numbersSet 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.
Can't use the interactive? Read a description instead

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.

Interactive figure (online). Description: 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

Everything runs in your browser. No kit, nothing mains-powered. Simulations are simplified educational models.

Guided practice

Answers are checked on the server; wrong answers get a hint and another try. Signed in, solved items also count towards the source lesson.

Practice 1

What is binary 1011 in decimal?

Practice 2

What is 0x2A in decimal?

Practice 3

Add 0110 + 0111 (4-bit). Give the result as a decimal number.

Practice 4

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?

delays

Practice 5

In a 4-bit unsigned register, what is 1011 + 0110? Give the value the register holds, in decimal.

Practice 6

What is −5 in 4-bit two's complement? Give the bit pattern's unsigned value in decimal.

Practice 7

Which 4-bit two's complement addition overflows?

  1. What is binary 1011 in decimal?
  2. What is 0x2A in decimal?
  3. Add 0110 + 0111 (4-bit). Give the result as a decimal number.
  4. 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? (delays)
  5. In a 4-bit unsigned register, what is 1011 + 0110? Give the value the register holds, in decimal.
  6. What is −5 in 4-bit two's complement? Give the bit pattern's unsigned value in decimal.
  7. Which 4-bit two's complement addition overflows?
    • 0011 + 1110 (3 + −2)
    • 0101 + 0100 (5 + 4)
    • 1111 + 1111 (−1 + −1)

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 verified videos. Nothing loads from YouTube until you press play.

Shows step by step how a handful of gates add binary numbers, using real chips you can buy.

13 min 38 s · captions on YouTube · source

Nothing loads from YouTube until you press play.

Shows how ones and zeros can stand for numbers and text, so binary stops feeling like magic.

10 min 45 s · captions on YouTube · source

Nothing loads from YouTube until you press play.

  • Learn how computers add numbers and build a 4 bit adder circuit (Ben Eater)
  • Representing Numbers and Letters with Binary: Crash Course Computer Science #4 (CrashCourse)

Chapter 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.
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.
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.
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.

Definitions from the 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

Keep your work in your Engineering Journal; it stays private unless you choose to share it.

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.

Publication record

Version
1
Status
Technically checked (automated checks only)
Technical review
Automated checks only, last run 2026-11-29. No human, educator or mentor review is claimed.src/content/textbooks/textbooks.test.ts: schema, every lesson, diagram, practice item, video, Field Guide entry, lab and reference resolves; figure numbers recomputed; no practice answers in client payloads.
Written by
Compiled and written by the Tactiverge team with AI assistance, from Tactiverge School lessons that were themselves written with AI assistance.
Licence
All rights reserved by the founders unless a licence is approved.
Corrections
None recorded yet.Spotted a mistake? Use “Report a problem” on any chapter, or see the corrections page.