Question
What are the three main encoding types used to represent information in computers?
Answer
Everything is stored as bits (each 0 or 1); the same bits mean different things depending on how they're interpreted. Integers use two encodings — unsigned and two's complement (signed).
A bit is the fundamental unit — physically just a voltage that's "low" (0) or "high" (1). On their own, bits carry no meaning; an encoding assigns meaning to a pattern.
| Encoding | Purpose | Examples |
|---|---|---|
| Unsigned | Non-negative integers | Memory addresses, array indices, sizes |
| Two's complement | Signed integers | Positive and negative numbers |
(Real numbers use a third family, floating point — base-2 scientific notation — but this topic focuses on the two integer encodings above.)
Why these integer encodings?
- Unsigned is the simplest — just count in binary — but it can't represent negatives.
- Two's complement handles negatives so that the same addition circuit works for both signed and unsigned values.
The key insight: Everything is bits - the pattern 11111111 is:
- 255 if interpreted as unsigned (8-bit)
- -1 if interpreted as two's complement
- Part of a float if interpreted as floating point
Why this matters:
- The bits don't "know" what they represent
- The TYPE in your code tells the CPU how to interpret them
- Bugs happen when you interpret bits with the wrong type!
Note saved — thanks!
Question
How do you convert between binary, decimal, and hexadecimal representations?
Answer
Each base is positional; the key shortcut is that one hex digit equals exactly four bits, so you can convert hex↔binary in 4-bit groups.
| Binary | Hex | Decimal |
|---|---|---|
| 0000 | 0 | 0 |
| 0001 | 1 | 1 |
| 0100 | 4 | 4 |
| 1001 | 9 | 9 |
| 1010 | A | 10 |
| 1111 | F | 15 |
Conversion shortcuts:
- Binary → Hex: Group 4 bits, convert each group
- Hex → Binary: Expand each hex digit to 4 bits
0xFA1D37B = 1111 1010 0001 1101 0011 0111 1011
Tip: Each hex digit = exactly 4 bits. Memorize A=10, B=11, C=12, D=13, E=14, F=15.
Go deeper:
Hexadecimal (Wikipedia) — the base-16 system and its 4-bits-per-digit relationship to binary.
Note saved — thanks!