3.1.3 · D3Boolean Algebra & Logic Gates

Worked examples — Two's complement signed numbers

2,369 words11 min readBack to topic

This page is the drill-ground for Two's complement signed numbers. We will hit every kind of situation the topic can throw at you — positive, negative, zero, the awkward edge values, subtraction, overflow, a real-world word problem, and an exam-style trap. Nothing here contradicts the parent note; it goes deeper by making you work each case by hand.

Before you start, you only need three facts from the parent, restated in plain words:

Recall The three tools we reuse everywhere
  • Weight rule (decode): the top bit is worth ; every other bit is worth . To decode, add up the weights of the bits that are 1.
  • Negate rule (encode ): flip every bit, then add 1 (because , so ).
  • Overflow rule: for signed add, overflow happens when carry into the top bit carry out of it: .

Here means "bitwise NOT of " (turn every 0 into 1 and vice-versa), and is how many bits we have. Unless stated we use , so the range is .


The scenario matrix

Every problem in this topic is one of these cells. The examples below are tagged with the cell they cover, so you can see the whole map is filled.

# Case class What's tricky about it Example
A Encode a positive number Just write the binary — sign bit stays 0 Ex 1
B Encode a negative number Flip-and-bump Ex 1
C Decode a bit-pattern with MSB = 0 Behaves like plain unsigned Ex 2
D Decode a bit-pattern with MSB = 1 Top bit is , not Ex 2
E Zero and its "double" 10000000 Only one zero; the extreme negative Ex 3
F Add two numbers, no overflow, carry-out dropped Discard carry safely Ex 4
G Subtraction as add-the-negative Ex 5
H Overflow, two positives Same-sign in, opposite-sign out Ex 6
I Overflow, two negatives The symmetric failure Ex 7
J Degenerate negate: Has no positive twin Ex 8
K Real-world word problem Translate a story into signed bytes Ex 9
L Exam twist: different bit-width Rules scale, numbers don't Ex 10

The number line above is the mental picture for all these cells: eight-bit patterns laid on a circle, the top half re-labelled as negatives.


Ex 1 — Encoding both signs (cells A & B)


Ex 2 — Decoding both MSB cases (cells C & D)


Ex 3 — Zero and the extreme negative (cell E)


Ex 4 — Addition, no overflow, carry dropped (cell F)


Ex 5 — Subtraction via add-the-negative (cell G)


Ex 6 — Overflow, two positives (cell H)


Ex 7 — Overflow, two negatives (cell I)


Ex 8 — The degenerate negate (cell J)


Ex 9 — Real-world word problem (cell K)


Ex 10 — Exam twist: a different width (cell L)


Recall Self-test before moving on

Decode 11011011 (8-bit). ::: Negate in 8-bit. ::: (overflow, no positive twin) Does 10000000 mean "minus zero"? ::: No — it's ; two's complement has a single zero in 8-bit signed gives what stored value, and is it valid? ::: 10010110 ; overflow, invalid Why is carry-out unreliable for signed overflow? ::: Ex 7 has carry-out yet is correct-shaped by carry only; use


Connections

  • Parent: Two's complement signed numbers
  • Binary number system — every encode step is ordinary binary underneath
  • Sign-magnitude representation — where "" and mirror-image intuition come from (and fail here)
  • One's complement — flip-only, the step before the "+1"
  • Full adder — the single circuit doing all additions above
  • Modular arithmetic — why dropped carries and wrap-arounds are exact
  • Overflow and carry flags — the ALU status bits Ex 6, 7, 10 set