How can the same 32 bits hold the distance between galaxies and the diameter of an atom? We dissect IEEE 754: sign, biased exponent and significand with a hidden bit, hand conversions, infinity, NaN, subnormals and machine epsilon.
Why doesn’t 0.1 plus 0.2 give 0.3? Because 0.1 is a repeating fraction in binary. We convert fractions between bases by successive multiplications, look at the bits of a float with struct, and see two practical ways out: integers for money and math.isclose for comparisons.
In Python, 5 and 6 returns 6, and 5 & 6 returns 4. We explain the difference and the bitwise operators AND, OR, XOR, NOT and the shifts, used in masks that check, set, clear and toggle bits. In the end, we see where the 755 in chmod comes from.
To subtract, the processor adds the first number to the complement of the second, so a single adder does both operations. We will see two’s complement, why 8 signed bits range from -128 to +127, and how overflow took down the Ariane 5 and froze Pac-Man on level 256.
The addition and subtraction rules of base 10 hold in any base: only the value that triggers the carry changes. We do arithmetic in binary, octal, and hexadecimal, see why with three addends the carry can be worth 2, and implement both algorithms in Python.
In any base, the value of the base itself is written 10. We start from positional notation to convert integers between decimal, binary, octal, and hexadecimal, using expansion, successive divisions, bit grouping, and Python’s bin, oct, hex, and int functions.