CAComputer Architecture · Lesson 1 of 7

Bits, Bytes & Binary

Everything in a computer — numbers, text, photos, this webpage — is stored as bits: billions of tiny switches that are either on or off. Learn to count the way the machine does.

A bit is a single 0 or 1. Eight bits make a byte, which can represent 256 different values (2^8). Binary is just place-value counting with 2s instead of 10s: the binary number 1011 means 1×8 + 0×4 + 1×2 + 1×1 = 11. Hexadecimal (base 16, digits 0-9 then a-f) is shorthand — one hex digit is exactly four bits, so 0xFF is 11111111 is 255.

Python
# Explore binary in any language — Python shown:
print(bin(11))       # 0b1011
print(0b1011)        # 11
print(hex(255))      # 0xff
print(0xff)          # 255

# Bitwise operators work directly on the bits:
a, b = 0b1100, 0b1010
print(bin(a & b))    # 0b1000  AND — both bits set
print(bin(a | b))    # 0b1110  OR  — either bit set
print(bin(a ^ b))    # 0b0110  XOR — exactly one set
print(bin(a << 1))   # 0b11000 shift left = ×2
print(bin(a >> 2))   # 0b11    shift right = ÷4

Negative numbers use two's complement: to negate, flip every bit and add one. The top bit acts as the sign. This is why an 8-bit signed integer ranges from -128 to 127, and why integer overflow wraps around to a huge negative number — the bits just carried past the sign position.

Text
8-bit two's complement:
  0000 0101  =  5
  1111 1011  = -5   (flip all bits of 5, add 1)
  0111 1111  =  127 (biggest positive)
  1000 0000  = -128 (most negative)
  0111 1111 + 1 = 1000 0000   -> 127 + 1 = -128. Overflow!

Text is numbers too (ASCII/Unicode):
  'A' = 65 = 0100 0001
  'a' = 97 = 0110 0001   (one bit apart — that's why
                          case-toggling is a single XOR)
◆ Note
Floating point (float/double) stores numbers as sign × fraction × 2^exponent — like scientific notation in binary. 0.1 has no exact binary representation, which is why 0.1 + 0.2 != 0.3 in every language. It's the format (IEEE 754), not the language.