Two's Complement & Overflow

Created by Sam Li

Learning Goals

Understand how computers represent positive and negative numbers. Learn the concept of Two's Complement and the role of the Sign Bit. Observe how overflow occurs when values exceed the physical memory limit.

Decimal (Input) Type a number & press Enter
8-bit Binary Representation
Sign(-128)
 (64)
 (32)
 (16)
 (8)
 (4)
 (2)
 (1)

Positional Weight & Range Formula

1. Calculating Value (Positional Weight)

In Two's Complement, the highest bit (MSB) carries a negative weight. For an 8-bit number, the MSB is worth -128, while the rest are positive (64, 32... 1).

Value = (-128 × b₇) + (64 × b₆) + ... + (1 × b₀)

2. Value Range

For an n-bit system, the formula to find the min and max values is [-2ⁿ⁻¹, 2ⁿ⁻¹ - 1].

For 8 bits (n = 8): [-2⁷, 2⁷ - 1] ➔ [-128, 127]

Understanding the Concepts

Why Two's Complement?

Two's complement is the standard way computers represent signed (positive and negative) integers. It is brilliant because it allows addition and subtraction to be performed by the exact same hardware logic circuit. It also eliminates the confusing problem of having two representations for zero (+0 and -0) which existed in older systems.

The Role of the Sign Bit

The Most Significant Bit (MSB, the leftmost bit) determines the sign. If it's 0, the number is positive. If it's 1, the number is negative. In Two's Complement, this bit acts as a large negative weight.

What is Overflow?

Overflow happens when a value exceeds the storage capacity of its bits. For an 8-bit signed integer, adding 1 to the maximum (127) causes the sign bit to flip, incorrectly wrapping the result around to the minimum (-128).

How to manually calculate?

1. Start with the positive binary number.

2. Invert all bits (0 becomes 1, 1 becomes 0).

3. Add 1 to the result.

Try it: Type 5, click "Invert Bits", then click "+ 1". You get -5!

Common Misconceptions

  • "To get a negative number, just change the first bit to 1."

    False. That is "Sign-and-Magnitude" representation, not Two's Complement. In Two's Complement, you must invert ALL bits and add 1.

  • "Two's complement has a +0 and a -0."

    False. One of its greatest advantages is having only ONE representation for zero (00000000), freeing up memory to store an extra negative number (-128).

Think & Link

  • How does the representable range change when upgrading from an 8-bit to a 16-bit system?
  • How do programming data types (like int or short) relate to this concept?
  • Why could an unexpected overflow crash a software system (like a video game or financial app)?

Answers:

  • Range Change: It grows exponentially! An 8-bit system holds 256 values (-128 to 127), but a 16-bit system holds 65,536 values (-32,768 to 32,767).
  • Data Types: Types like short (usually 16-bit) or int (usually 32-bit) are just memory "buckets" of different sizes. Choosing the right bucket prevents data from spilling over.
  • System Crashes: If a variable exceeding its limit wraps around to a negative number or zero, this sudden, illogical change breaks the program's mathematical logic and causes critical crashes!