Skip to content

Try “polymorphism”, “TCP handshake” or “git rebase”.

    Computer Basics / How Computers Work

    Binary Numbers

    Our normal counting system is decimal or base 10.

    Chapter 2 of 96 min read

    The Plain Picture#

    Think about how you already count in normal numbers. The number 305 means:

    Output
      3 hundreds  +  0 tens  +  5 ones
      3 × 100     +  0 × 10  +  5 × 1   = 305

    Each position is worth 10 times the position to its right (1, 10, 100, 1000…). We use 10 because we have 10 digits (0–9) — probably because humans have 10 fingers.

    Binary is exactly the same idea, but with only 2 digits (0 and 1). So each position is worth 2 times the position to its right.

    The Real Name#

    • Our normal counting system is decimal or base 10.

    • Counting with only 0 and 1 is binary or base 2.

    • A group of 8 bits is a byte. The byte is the basic unit computers use to measure data.

    Under the Hood#

    Position values in binary#

    Output
      position value:   128   64   32   16    8    4    2    1
                        2⁷    2⁶   2⁵   2⁴    2³   2²   2¹   2⁰

    Binary → decimal (reading a binary number)#

    Take 1101. Line it up under the position values from the right:

    Output
      bit:              1    1    0    1
      position value:   8    4    2    1
      bit × value:      8  + 4  + 0  + 1  = 13

    So binary 1101 is decimal 13.

    Decimal → binary (writing a number in binary)#

    Let's store the quiz score 200. Method: take the biggest position value that fits, subtract, repeat.

    Output
      200 ≥ 128?  yes → bit 1,  remaining 200 − 128 = 72
       72 ≥  64?  yes → bit 1,  remaining  72 −  64 =  8
        8 ≥  32?  no  → bit 0
        8 ≥  16?  no  → bit 0
        8 ≥   8?  yes → bit 1,  remaining   8 −   8 =  0
        0 ≥   4?  no  → bit 0
        0 ≥   2?  no  → bit 0
        0 ≥   1?  no  → bit 0
    
      200 in binary = 1100 1000

    Check: 128 + 64 + 8 = 200 ✔

    One byte = 0 to 255#

    8 bits give 2⁸ = 256 combinations. If we start counting at 0, one byte holds 0 to 255:

    Output
      0000 0000  =   0
      0000 0001  =   1
      1111 1111  = 128+64+32+16+8+4+2+1 = 255

    Sizes: KB, MB, GB#

    NameShortRoughlyExactly (the way memory chips count)
    byteB8 bits8 bits
    kilobyteKBa thousand bytes1,024 bytes (2¹⁰)
    megabyteMBa million bytes1,048,576 bytes (2²⁰)
    gigabyteGBa billion bytes2³⁰ bytes
    terabyteTBa trillion bytes2⁴⁰ bytes

    There are two slightly different conventions:

    • Disk makers and networks use 1 KB = 1,000 bytes (powers of 10).

    • Memory (RAM) and many operating systems use 1,024 (powers of 2). The strict name for 1,024 bytes is KiB ("kibibyte"), 2²⁰ is MiB, and so on.

    This is why a "512 GB" disk shows up as about 476 GB on your computer: the disk maker counted 512 × 1,000,000,000 bytes, and your computer divides by 1,073,741,824 (2³⁰).

    Bits vs bytes in speeds: internet speeds are sold in bits per second (Mbps, small b). Files are measured in bytes (MB, big B). A 100 Mbps connection downloads at most 100 ÷ 8 = 12.5 MB per second.

    Overflow — when the number doesn't fit#

    What is 255 + 1 in a single byte?

    Output
        1111 1111   (255)
      + 0000 0001   (1)
      -----------
      1 0000 0000   ← needs 9 bits, but a byte has only 8

    The 9th bit has nowhere to go and is thrown away. The byte now holds 0000 0000 = 0. This wrap-around is called overflow. It is like a car's odometer rolling from 99999 to 00000.

    Gotchas

    • Overflow does not crash the program in most languages, including Java. The number silently wraps around and your program keeps going with a wrong value. You will see this in Chapter 4 with real Java numbers.

    • Mbps vs MB/s confusion is common in system design estimates. Always write the unit.

    Check yourself

    4 questions
    1

    Convert binary 1010 1010 to decimal.

    Show answer

    128 + 32 + 8 + 2 = 170.

    2

    Convert decimal 100 to binary.

    Show answer

    64 + 32 + 4 = 100 → 0110 0100.

    3

    What is the biggest number 10 bits can hold (starting from 0)?

    Show answer

    2¹⁰ − 1 = 1,023.

    4

    A file is 50 MB. Your internet is 200 Mbps. Roughly how long does the download take?

    Show answer

    200 Mbps ÷ 8 = 25 MB/s. 50 MB ÷ 25 MB/s = 2 seconds.