Data Representation in Memory: Binary, Two's Complement, and Encoding Standards
Inside a digital computer, there are no letters, no colors, no negative signs, and no sound waves. The hardware consists solely of microscopic semiconductor switches and capacitors that hold high or low voltage states: binary $1$s and $0$s.
How then does a computer perform advanced mathematical physics, display ultra-high-definition video, or render Nigerian languages like Yoruba, Hausa, and Igbo with accurate diacritics?
The answer lies in Formal Data Representation Schemes. In this lesson, we study how computers encode signed negative integers, handle arithmetic overflow, represent text with modern Unicode, and sample continuous analog reality into digital discrete values.
Learning Outcomes
By the end of this lesson, you will be able to:
- Convert signed integers using the Two's Complement algorithm.
- Identify Integer Overflow in fixed-width arithmetic.
- Trace character encoding evolution from 7-bit ASCII to variable-width UTF-8 Unicode.
- Calculate digital media sizes for bitmap images (bit depth) and PCM audio (sampling rate and bit resolution).
Key Vocabulary
| Term | Technical Definition |
|---|---|
| Most Significant Bit (MSB) | The leftmost bit in a binary word; in signed integer systems, it serves as the sign flag ($0$ for positive, $1$ for negative). |
| Two's Complement | The mathematical system used by computer ALUs to represent signed negative numbers, enabling addition and subtraction with identical hardware circuits. |
| Arithmetic Overflow | An error condition occurring when an arithmetic calculation produces a result too large to fit in the allocated register bit-width. |
| ASCII | American Standard Code for Information Interchange; a 7-bit character set representing 128 English alphanumeric characters. |
| Unicode (UTF-8) | A variable-width character encoding standard capable of representing over 1.1 million symbols across all human languages. |
| Bit Depth | The number of binary bits allocated to represent the color of a single image pixel or the amplitude of a sound sample. |
Signed Numbers and Two's Complement
In primary computing, you learned how to convert positive integers to binary (e.g., $13_{10} = 00001101_2$ in 8-bit). But how do we represent $-13$?
Computer engineers use Two's Complement. This mathematical convention has a huge advantage: the CPU can subtract numbers by simply adding their Two's Complement! The arithmetic logic unit needs no separate subtraction circuitry.
The 3-Step Two's Complement Algorithm:
To convert a negative decimal number $-N$ into an 8-bit signed binary representation: 1. Write the positive binary representation of $+N$ in 8 bits. 2. Invert every bit ($0 \rightarrow 1$ and $1 \rightarrow 0$). This produces the One's Complement. 3. Add $1$ to the resulting binary number using standard binary addition.
Worked Example: Convert $-19_{10}$ to 8-Bit Two's Complement
Step 1: Write +19 in 8-bit binary:
19 = 16 + 2 + 1
Binary: 0 0 0 1 0 0 1 1
Step 2: Invert all bits (One's Complement):
00010011 ===> 1 1 1 0 1 1 0 0
Step 3: Add 1:
1 1 1 0 1 1 0 0
+ 1
-----------------
1 1 1 0 1 1 0 1 (Two's Complement representation of -19)
Notice that the MSB (leftmost bit) is 1, indicating that the number is negative.
Verifying Signed Range:
In an $N$-bit signed integer register:
$$\text{Range} = -2^{N-1} \text{ to } (2^{N-1} - 1)$$
For an 8-bit register:
$$\text{Minimum} = -2^7 = -128$$
$$\text{Maximum} = +2^7 - 1 = +127$$
Integer Overflow: When Limits Are Exceeded
What happens if an 8-bit signed register holding $+120$ adds $+15$? Mathematically, $120 + 15 = 135$. But the maximum positive value in an 8-bit signed register is $+127$!
0 1 1 1 1 0 0 0 (+120)
+ 0 0 0 0 1 1 1 1 (+15)
-----------------
1 0 0 0 0 1 1 1 (Bit 7 flipped to 1!)
Because bit 7 flipped to 1, the CPU interprets this binary pattern as a negative number ($-121$)! This silent error is called Arithmetic Overflow. It has caused real-world disasters, including the explosion of the European Ariane 5 rocket in 1996 when a 64-bit floating-point number was cast into a 16-bit signed integer.
Text Encoding: From ASCII to Global UTF-8
Computers cannot store characters like 'A' or '₦' directly. They assign each character a numeric integer code.
Character Code Schemes:
1960s: Standard ASCII (7-bit) --> 128 characters (A-Z, a-z, 0-9, basic punctuation)
1980s: Extended ASCII (8-bit) --> 256 characters (Western European accents)
1990s-Present: Unicode UTF-8 --> 1,114,112 code points (Global scripts, Diacritics, Emoji)
Why UTF-8 is Revolutionary:
- Backward Compatibility: ASCII codes 0 to 127 are identical in UTF-8 and require exactly 1 byte.
- Variable Width: Common characters use 1 byte; African and European accented characters (like
ẹ,ọ,é) use 2 bytes; Asian characters use 3 bytes; complex emoji (like🚀or🎓) use 4 bytes. - Bandwidth Efficiency: English web text remains compact without wasting 4 bytes per letter.
Practical Activity: Binary Two's Complement and Hex Inspector in Python
Let's write a practical Python script to inspect how integers are stored in binary, how bitwise operations work, and how UTF-8 decodes multi-byte characters.
Step 1: Create data_rep_lab.py
# Function to inspect 8-bit signed integer representation
def inspect_signed_8bit(val):
if not -128 <= val <= 127:
return "Value out of 8-bit signed range (-128 to 127)"
# Mask with 0xFF to view the raw 8 bits in memory
raw_bits = val & 0xFF
binary_str = f"{raw_bits:08b}"
hex_str = f"0x{raw_bits:02X}"
msb = binary_str[0]
return f"Decimal: {val:>4} | Binary: {binary_str} | Hex: {hex_str} | Sign Bit: {msb}"
print("--- 8-Bit Signed Two's Complement Inspection ---")
for number in [0, 1, 19, -19, 127, -128, -1]:
print(inspect_signed_8bit(number))
print("\n--- Unicode UTF-8 Byte Inspection ---")
characters = ["A", "₦", "ẹ", "💻"]
for char in characters:
encoded_bytes = char.encode("utf-8")
byte_hex = " ".join([f"{b:02X}" for b in encoded_bytes])
print(f"Char: {char} | Codepoint: U+{ord(char):04X} | Bytes ({len(encoded_bytes)}): {byte_hex}")
Step 2: Execute the Script
Run the script in your terminal:
Expected Results and Model Output
When you run data_rep_lab.py, your terminal will display:
--- 8-Bit Signed Two's Complement Inspection ---
Decimal: 0 | Binary: 00000000 | Hex: 0x00 | Sign Bit: 0
Decimal: 1 | Binary: 00000001 | Hex: 0x01 | Sign Bit: 0
Decimal: 19 | Binary: 00010011 | Hex: 0x13 | Sign Bit: 0
Decimal: -19 | Binary: 11101101 | Hex: 0xED | Sign Bit: 1
Decimal: 127 | Binary: 01111111 | Hex: 0x7F | Sign Bit: 0
Decimal: -128 | Binary: 10000000 | Hex: 0x80 | Sign Bit: 1
Decimal: -1 | Binary: 11111111 | Hex: 0xFF | Sign Bit: 1
--- Unicode UTF-8 Byte Inspection ---
Char: A | Codepoint: U+0041 | Bytes (1): 41
Char: ₦ | Codepoint: U+20A6 | Bytes (3): E2 82 A6
Char: ẹ | Codepoint: U+1EB9 | Bytes (3): E1 BA B9
Char: 💻 | Codepoint: U+1F4BB | Bytes (4): F0 9F 92 BB
Notice how -1 is stored as all ones (11111111), and the Nigerian Naira symbol (₦) requires 3 bytes in UTF-8!
Common Mistakes to Avoid
- Assuming Positive and Negative Numbers Use the Same Magnitude Bits with Just a Sign Bit: Pure "sign-and-magnitude" is not used in modern CPUs because it creates two representations for zero ($+0$ and $-0$) and requires complicated subtraction circuits. Two's Complement has only one zero and simplifies hardware.
- Confusing Character Codes with Numerical Values: The ASCII character
'5'has decimal code $53$ (00110101in binary). The numerical integer $5$ is stored as00000101. Mixing them up in low-level code produces severe bugs. - Assuming 1 Character Always Equals 1 Byte: In Python 3 and modern web software, characters are variable-length Unicode. Assuming a string of 10 characters is always 10 bytes on disk or network streams can corrupt multi-byte text.
Notebook Summary
- Two's Complement is the standard for signed integer arithmetic. Subtraction is performed by adding the inverted-plus-one negative representation.
- In $N$-bit signed systems, the range is $-2^{N-1}$ to $+2^{N-1}-1$. Exceeding this causes Arithmetic Overflow.
- ASCII uses 7 bits ($0-127$). UTF-8 is a backward-compatible variable-length standard ($1-4$ bytes) representing over 1.1 million international symbols.
- Digital media is quantized: Bit Depth determines image color precision and audio dynamic range.
Optional Extension Challenge
Write a Python function add_8bit_signed(a, b) that performs addition of two integers using 8-bit Two's Complement logic, tests for arithmetic overflow, and prints whether the CPU's overflow flag (V) would be set to 1 or 0.