In this lesson
- Use two’s complement and detect overflow
- Explain IEEE 754 floating point and its rounding
Explanation
Signed integers use two’s complement: an 8-bit byte holds −128 to 127, and adding past the range wraps around. Floating-point numbers store a sign, exponent and fraction, so most decimal fractions are approximations. Text is stored as Unicode code points, usually encoded as UTF-8.
Worked example
−5 in 8-bit two’s complement: invert 00000101 → 11111010, add 1 → 11111011. In Python, 0.1 + 0.2 == 0.3 is False because neither 0.1 nor 0.2 is exact in binary.
Practice
Convert −37 to 8-bit two’s complement and explain why summing money in floating point is risky.