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.
Overflow Occurred!
The value exceeded the 8-bit signed range (-128 to 127) and wrapped around.
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).
For an n-bit system, the formula to find the min and max values is [-2ⁿ⁻¹, 2ⁿ⁻¹ - 1].
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 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.
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).
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!
False. That is "Sign-and-Magnitude" representation, not Two's Complement. In Two's Complement, you must invert ALL bits and add 1.
False. One of its greatest advantages is having only ONE representation for zero (00000000), freeing up memory to store an extra negative number (-128).
If we upgrade a system from 8-bit to 16-bit, how does the representable range change? Think about how data types (like int or short in programming) relate to this concept, and why an unexpected overflow could crash a video game or a financial system!