Project 4: Two’s-Complement Range Explorer

  • File: P04-twos-complement-explorer.md
  • Main Programming Language: C or Python
  • Alternative Programming Languages: Rust, Go, Java
  • Coolness Level: Level 3 (See REFERENCE.md)
  • Business Potential: Level 1 (See REFERENCE.md)
  • Difficulty: Level 2 (See REFERENCE.md)
  • Knowledge Area: Signed Integers
  • Software or Tool: CLI
  • Main Book: “Computer Systems: A Programmer’s Perspective”

What you will build: A tool that prints the signed and unsigned interpretation of the same bit pattern for multiple bit widths.

Why it teaches binary/hex: Two’s complement shows how interpretation changes meaning while bits stay the same.

Core challenges you will face:

  • Range calculation -> Two’s Complement
  • Sign extension -> Two’s Complement
  • Overflow detection -> Two’s Complement

Real World Outcome

$ twos-explore --width 8 --value 0xFF
unsigned: 255
signed:   -1
binary:   11111111
range:    -128 .. 127

The Core Question You Are Answering

“How can the same bits mean two different numbers?”

Concepts You Must Understand First

  1. Two’s complement
    • Why is it the standard for signed integers?
    • Book Reference: “Computer Systems: A Programmer’s Perspective” - Ch. 2
  2. Bit width
    • How does width determine range?
    • Book Reference: “Computer Systems: A Programmer’s Perspective” - Ch. 2

Questions to Guide Your Design

  1. Input
    • Will you accept hex, binary, and decimal?
    • How will you normalize to a fixed width?
  2. Output
    • How will you show signed/unsigned side by side?
    • Will you display range boundaries?

Thinking Exercise

Signed vs Unsigned Table

Make a table of 4-bit values (0x0 to 0xF) and annotate their signed meaning.

Questions to answer:

  • Why does 0x8 become negative?
  • Which value has no positive counterpart?

The Interview Questions They Will Ask

  1. “Explain two’s complement in your own words.”
  2. “Why is the signed range asymmetric?”
  3. “What is sign extension and why does it matter?”
  4. “How do you detect signed overflow?”
  5. “What is the signed value of 0x80 in 8-bit?”

Hints in Layers

Hint 1: Starting Point Mask the input to the requested width.

Hint 2: Next Level If the sign bit is set, compute negative value by invert+1.

Hint 3: Technical Details Pseudocode:

if sign_bit_set(value, width):
  signed = -((invert(value) + 1) within width)
else:
  signed = value

Hint 4: Tools/Debugging Cross-check with a language that lets you set fixed-width integers.

Books That Will Help

Topic Book Chapter
Integer representation “Computer Systems: A Programmer’s Perspective” Ch. 2

Common Pitfalls and Debugging

Problem 1: “Negative values look wrong”

  • Why: You are not masking to width before interpreting.
  • Fix: Apply a width mask before any sign logic.
  • Quick test: 0xFF at width 8 should be -1.

Definition of Done

  • Shows signed and unsigned for any width 4-32
  • Displays binary form with sign bit highlighted
  • Handles overflow boundaries correctly
  • Includes a test table for 4- and 8-bit widths