Number Systems - Binary, Octal, Decimal & Hexadecimal
How computers represent numbers - and how to convert between the bases that matter in CS.
1 The Four Number Systems
11111111₂ = FF₁₆ = 255₁₀.2 Positional Value - How Bases Work
In any base, each digit's value = digit × base^position (counting from 0 on the right).
Binary to Decimal Conversion
Bit: 1 0 1 1 0 1 0 1 Position: 7 6 5 4 3 2 1 0 Value: 128 64 32 16 8 4 2 1
Decimal to Binary Conversion
200 ÷ 2 = 100 remainder 0 100 ÷ 2 = 50 remainder 0 50 ÷ 2 = 25 remainder 0 25 ÷ 2 = 12 remainder 1 12 ÷ 2 = 6 remainder 0 6 ÷ 2 = 3 remainder 0 3 ÷ 2 = 1 remainder 1 1 ÷ 2 = 0 remainder 1
Hexadecimal Conversions
To binary: split each hex digit into 4 bits:
3 Two's Complement - Signed Integers
Computers represent negative numbers using two's complement. The leftmost bit is the sign bit (0 = positive, 1 = negative).
// Example: −42 in 8-bit two's complement 42₁₀ = 00101010₂ Flip: 11010101 Add 1: 11010110 // −42₁₀ = 11010110₂