Bits and Number Bases: Binary, Hex, Two’s Complement

What this episode covers

Everything a computer stores ends up as bits. Each position has a weight and the lit bits add up to the number; make the top weight negative and you get two’s complement, so the same bits can also express negative numbers.

Grouping four bits into one hexadecimal digit makes long binary strings readable. In this episode you flip the bits yourself and check what the same string means under each of the three readings.

Prerequisites

No prerequisites: you can start right here.

Where it is used in the real world

  • C / Rust integer types Types like int8_t and i32 are fixed-width two’s complement numbers; wrap-around on overflow is exactly the 15 plus 1 becoming 0 you saw here.
  • Colour codes like #4FD1C5 Web colours are written in hex, and each pair of hex digits is the 8 bits of one red, green or blue channel.
  • IPv4 and MAC addresses Network addresses are just strings of bits, written in decimal or hex, and masks use bitwise operations to decide whether two machines share a network.

Watch next

  • Floating Point: Why 0.1 + 0.2 ≠ 0.3 Coming soon
  • From Logic Gates to an Adder Coming soon

Episode 1

This is episode 1 of the “How Computers “Think”” series (12 planned).

See all episodes in the series