This lesson includes an expert video walkthrough — purchase once for a full year of unlimited replays to master every key point 🎬
Computer Encoding
Knowledge Summary
Representation of Machine Numbers
Computers use binary to represent data. The highest bit is the sign bit: 0 for positive and 1 for negative.
Three Encoding Methods
Using 8 bits as an example:
| Value | Sign-Magnitude | One's Complement | Two's Complement |
|---|---|---|---|
| +5 | 0000 0101 | 0000 0101 | 0000 0101 |
| -5 | 1000 0101 | 1111 1010 | 1111 1011 |
| +0 | 0000 0000 | 0000 0000 | 0000 0000 |
| -0 | 1000 0000 | 1111 1111 | 0000 0000 |
Conversion Rules
- Positive numbers: sign-magnitude = one's complement = two's complement
- Negative numbers:
- Sign-magnitude -> one's complement: keep the sign bit unchanged and invert the remaining bits
- One's complement -> two's complement: add 1
- Sign-magnitude -> two's complement: invert bits and add 1
Why Use Two's Complement
- It resolves the ambiguity between +0 and -0
- Subtraction can be implemented with addition: a - b = a + (two's complement of -b)
- It simplifies hardware circuit design
Range of Two's Complement Representation (n bits)
- Signed integer range: -2^(n-1) to 2^(n-1) - 1
- 8 bits: -128 to 127
- 16 bits: -32768 to 32767
- 32 bits: about -2.1×10⁹ to 2.1×10⁹
ASCII Codes (Frequently Tested)
| Character | ASCII Code |
|---|---|
| '0' | 48 |
| 'A' | 65 |
| 'a' | 97 |
| Space | 32 |
'a' - 'A' = 32(case conversion)- The ASCII code of
'0'is 48, not 0