Computational Thinking and Foundations
Build core mental models for problem solving, data representation, and the computing ecosystem.
Content
Bits, Bytes, and Binary
Versions:
Watch & Learn
AI-discovered learning video
Sign in to watch the learning video for this topic.
Bits, Bytes, and Binary — The Secret Language of Computers
You read the History of Computing already, so you know where the machines came from. Now let's peek under the hood and see how they actually speak.
Computers don't do philosophy or poetry. They do decisions: on/off, yes/no, 1/0. Those tiny choices are bits — the atomic unit of information. When bits team up, they form bytes, and bytes are the bricks that build everything digital: numbers, text, images, sound, and yes — memes.
Why this matters (even if you plan to live in a high-level language forever)
- Debugging superpowers: When you hit weird behavior, understanding bits helps you read the error message in the computer's tone of voice.
- Storage, performance, and limits: Every GB, overflow, and memory bug traces back to binary choices.
- Interview friendliness: CS50 exercises and interviews love questions about binary, two's complement, and bitwise ops.
Think of this as the difference between learning how to operate a car and actually understanding why the engine knocks. You don't need to rebuild the engine, but knowing the basics prevents incredulous errors like storing 300 in an 8-bit box.
The basics: bit and byte
- Bit = binary digit = either 0 or 1. Imagine a tiny light switch.
- Byte = typically 8 bits. Picture a small row of 8 light switches.
Why 8? Historical reasons (balance of hardware simplicity and usefulness) — but the practical result: a single byte can represent 256 distinct values (2^8 = 256).
Micro explanation: Powers of two table (quick reference)
| Bits | Distinct values | Common name |
|---|---|---|
| 1 | 2 | - |
| 4 | 16 | nibble |
| 8 | 256 | byte |
| 16 | 65,536 | 2 bytes |
| 32 | ~4.3 billion | 4 bytes |
| 64 | ~1.8e19 | 8 bytes |
Binary numbers — how counting works with only 0 and 1
Decimal uses base 10 because humans like ten fingers. Computers use base 2.
Example: decimal 13 in binary
- Write powers of two: 8, 4, 2, 1
- 13 = 8 + 4 + 1
- So binary: 1101
Binary place values (from right): 1, 2, 4, 8, 16, 32...
Quick conversion cheats
- Decimal to binary: subtract largest power of two, mark a 1, continue.
- Binary to decimal: sum the place values where bits are 1.
Code toy (Python-ish):
# decimal to binary (simple)
def dec_to_bin(n):
if n == 0:
return '0'
bits = []
while n > 0:
bits.append(str(n % 2))
n //= 2
return ''.join(reversed(bits))
print(dec_to_bin(13)) # prints 1101
(Using single quotes to avoid JSON double-quote drama.)
Bytes as containers: characters, integers, and images
- A single byte can store 0-255 (unsigned) — useful for colors (0-255 per color channel) and ASCII characters.
- ASCII uses 1 byte per character for basic English text. Extended encodings like UTF-8 can use multiple bytes for other scripts.
Analogy: A byte is a small box where you can put one of 256 different toys. If you need more toys (bigger numbers or characters), you glue boxes together (multi-byte values).
Character example: ASCII
- 'A' -> 65 -> 01000001
- 'a' -> 97 -> 01100001
So, text is just bytes interpreted one way; images are bytes arranged and interpreted another way.
Signed numbers and two's complement (because negatives exist)
Computers represent negative numbers efficiently using two's complement. With 8 bits:
- Range for signed 8-bit: -128 to +127
- Positive numbers: standard binary.
- Negative numbers: invert bits and add 1 (two's complement).
Example: represent -5 in 8-bit
- +5 = 00000101
- Invert -> 11111010
- Add 1 -> 11111011 = -5
Why care? Because overflow is sneaky: 127 + 1 -> -128 in 8-bit signed arithmetic. That's not a philosophical statement; it's just math.
Bitwise operations — the tiny tools that do big things
- AND (&): 1 only if both bits are 1. Use to mask bits.
- OR (|): 1 if either bit is 1. Combine flags.
- XOR (^): 1 if bits differ. Common in parity and crypto primitives.
- NOT (~): flips bits.
- Shifts (<<, >>): multiply/divide by powers of two (careful with signed numbers).
Practical uses:
- Masks: turn off/on specific bits (permissions, flags)
- Fast multiplication/division by powers of two
- Hashing and low-level compression tricks
Example: clear the lowest 3 bits of a number n: n & ~0b111
Endianness — which byte is first?
When values span multiple bytes, order matters. Two main conventions:
- Big-endian: most significant byte first (human-friendly)
- Little-endian: least significant byte first (Intel CPUs)
Think of writing a multi-syllable word either from left-to-right or right-to-left — both encode the same thing, but you must agree on the convention.
Common gotchas you will see in CS50
- Assuming a variable can hold arbitrary large integers in low-level contexts — watch out for overflow.
- Confusing bytes and bits when reading storage sizes: 1 byte = 8 bits. So a 1 Mb (megabit) network link is 1/8 the speed of a 1 MB (megabyte) file transfer.
- Mixing decimal KB vs binary KiB: 1 KB often = 1000 bytes (marketing), while 1 KiB = 1024 bytes (binary).
Summary — the memorable insight
Bits are tiny decisions; bytes are their neighborhood. Everything digital is built by grouping bits and agreeing on how to interpret them.
When you hit weird behavior, ask: how are the bits arranged, interpreted, and limited? That question often solves the problem faster than adding print statements.
This is the moment where it finally clicks: computers are not magic — they're extremely literal. They only know 0 and 1, and how we choose to read those 0s and 1s defines everything from text to video games.
Quick takeaways
- 1 bit = 0 or 1. 1 byte = 8 bits (256 values).
- Binary is base 2; use place values to convert.
- Two's complement represents signed integers; watch for overflow.
- Bitwise ops are powerful for flags, masks, and low-level tricks.
- Endianness = byte order; be consistent.
If you understood two's complement, masks, and why 8 bits gives 256 values, you just moved from tourist to local in Computer Town.
Happy hacking — and when in doubt, count the bits.
Comments (0)
Please sign in to leave a comment.
No comments yet. Be the first to comment!