Data Representation Basics
Data Representation Basics¶
Overview¶
Computers represent all data in binary (0s and 1s). In this lesson, we'll learn about number systems, base conversion methods, data units, and the concept of complements. This forms the foundation for understanding how computers process data.
Difficulty: β (Basic)
Table of Contents¶
- Understanding Number Systems
- Binary (Base-2)
- Octal (Base-8)
- Hexadecimal (Base-16)
- Base Conversion
- Data Units
- Concept of Complements
- Practice Problems
1. Understanding Number Systems¶
What is a Number System?¶
A number system is a way of representing numbers using a base (radix).
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Types of Number Systems β
βββββββββββββββ¬ββββββββββ¬ββββββββββββββββββββββββββββββββββ€
β System β Base β Digits Used β
βββββββββββββββΌββββββββββΌββββββββββββββββββββββββββββββββββ€
β Binary β 2 β 0, 1 β
β Octal β 8 β 0, 1, 2, 3, 4, 5, 6, 7 β
β Decimal β 10 β 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 β
β Hexadecimalβ 16 β 0-9, A, B, C, D, E, F β
βββββββββββββββ΄ββββββββββ΄ββββββββββββββββββββββββββββββββββ
Place Value Principle¶
In all number systems, each position has a value based on powers of the base.
Place values in decimal 1234:
1 2 3 4
10Β³ 10Β² 10ΒΉ 10β°
=1000 =100 =10 =1
1234 = 1Γ10Β³ + 2Γ10Β² + 3Γ10ΒΉ + 4Γ10β°
= 1000 + 200 + 30 + 4
= 1234
Place values in binary 1101:
1 1 0 1
2Β³ 2Β² 2ΒΉ 2β°
=8 =4 =2 =1
1101β = 1Γ2Β³ + 1Γ2Β² + 0Γ2ΒΉ + 1Γ2β°
= 8 + 4 + 0 + 1
= 13ββ
2. Binary (Base-2)¶
Binary Characteristics¶
Why computers use binary:
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Why Do Computers Use Binary? β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β 1. Corresponds to two states of electrical signals β
β (ON/OFF) β
β 2. Robust against noise (only need to distinguish β
β two states) β
β 3. Direct correspondence with logic operations β
β (true/false) β
β 4. Simpler circuit design β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Voltage
β
5V ββ€ ββββββββ ββββββββ
β β β
0V ββ€βββββββββββ΄ββββββββββ΄ββββββββββ Time
β
HIGH(1) LOW(0) HIGH(1)
Binary Notation¶
Binary notation methods:
- Subscript: 1010β
- Prefix: 0b1010 (commonly used in programming)
- Suffix: 1010b
Powers of 2 (recommended to memorize):
βββββββ¬ββββββ¬ββββββ¬ββββββ¬ββββββ¬ββββββ¬ββββββ¬ββββββ¬ββββββ
β 2β° β 2ΒΉ β 2Β² β 2Β³ β 2β΄ β 2β΅ β 2βΆ β 2β· β 2βΈ β
βββββββΌββββββΌββββββΌββββββΌββββββΌββββββΌββββββΌββββββΌββββββ€
β 1 β 2 β 4 β 8 β 16 β 32 β 64 β 128 β 256 β
βββββββ΄ββββββ΄ββββββ΄ββββββ΄ββββββ΄ββββββ΄ββββββ΄ββββββ΄ββββββ
βββββββ¬ββββββ¬βββββββ¬βββββββ¬ββββββββ
β 2βΉ β 2ΒΉβ° β 2ΒΉΒΉ β 2ΒΉΒ² β 2ΒΉβΆ β
βββββββΌββββββΌβββββββΌβββββββΌββββββββ€
β 512 β1024 β 2048 β 4096 β 65536 β
βββββββ΄ββββββ΄βββββββ΄βββββββ΄ββββββββ
Binary Addition¶
Binary addition rules:
0 + 0 = 0
0 + 1 = 1
1 + 0 = 1
1 + 1 = 10 (write 0, carry 1)
1 + 1 + 1 = 11 (write 1, carry 1)
Example: 1011 + 1101
1 1 (carry)
1 0 1 1
+ 1 1 0 1
βββββββββ
1 1 0 0 0
Verification: 11ββ + 13ββ = 24ββ = 11000β β
Binary Subtraction¶
Binary subtraction rules:
0 - 0 = 0
1 - 0 = 1
1 - 1 = 0
0 - 1 = 1 (borrow from higher bit)
Example: 1101 - 1011
0 10 (borrow)
1 1 0 1
- 1 0 1 1
βββββββββ
0 0 1 0
Verification: 13ββ - 11ββ = 2ββ = 10β β
3. Octal (Base-8)¶
Octal Characteristics¶
Octal uses digits 0-7.
Since 2Β³ = 8, three binary digits correspond to one octal digit.
Relationship between binary and octal:
ββββββββββ¬βββββββββ
β Binary β Octal β
ββββββββββΌβββββββββ€
β 000 β 0 β
β 001 β 1 β
β 010 β 2 β
β 011 β 3 β
β 100 β 4 β
β 101 β 5 β
β 110 β 6 β
β 111 β 7 β
ββββββββββ΄βββββββββ
Octal Notation¶
Octal notation methods:
- Subscript: 753β
- Prefix: 0o753 (Python) or 0753 (C)
Example: Binary β Octal
110 101 011β
6 5 3β = 653β
Example: Octal β Binary
752β = 111 101 010β
Uses of Octal¶
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Applications of Octal β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β 1. Unix/Linux file permissions (chmod 755) β
β - 755 = rwxr-xr-x β
β - 7 = 111β = rwx (read+write+execute) β
β - 5 = 101β = r-x (read+execute) β
β β
β 2. Legacy computer systems (PDP-8, etc.) β
β 3. ASCII code representation β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
4. Hexadecimal (Base-16)¶
Hexadecimal Characteristics¶
Hexadecimal uses 0-9 and A-F (10-15).
Since 2β΄ = 16, four binary digits correspond to one hex digit.
Relationship between binary and hexadecimal:
ββββββββββ¬βββββββββ¬βββββββββ
β Decimalβ Binary β Hex β
ββββββββββΌβββββββββΌβββββββββ€
β 0 β 0000 β 0 β
β 1 β 0001 β 1 β
β 2 β 0010 β 2 β
β 3 β 0011 β 3 β
β 4 β 0100 β 4 β
β 5 β 0101 β 5 β
β 6 β 0110 β 6 β
β 7 β 0111 β 7 β
β 8 β 1000 β 8 β
β 9 β 1001 β 9 β
β 10 β 1010 β A β
β 11 β 1011 β B β
β 12 β 1100 β C β
β 13 β 1101 β D β
β 14 β 1110 β E β
β 15 β 1111 β F β
ββββββββββ΄βββββββββ΄βββββββββ
Hexadecimal Notation¶
Hexadecimal notation methods:
- Subscript: 2AFββ
- Prefix: 0x2AF (most commonly used)
- Suffix: 2AFh
Example: Binary β Hexadecimal
1010 1111 0011β
A F 3ββ = 0xAF3
Example: Hexadecimal β Binary
0x3E8 = 0011 1110 1000β
Uses of Hexadecimal¶
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Applications of Hexadecimal β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β 1. Memory address representation β
β - 0x7FFE1234 β
β β
β 2. Color codes (RGB) β
β - #FF5733 = Red(FF) Green(57) Blue(33) β
β - Each color ranges from 0-255 (0x00-0xFF) β
β β
β 3. MAC addresses β
β - 00:1A:2B:3C:4D:5E β
β β
β 4. Machine code/assembly language β
β - MOV AX, 0x1234 β
β β
β 5. Unicode/ASCII codes β
β - 'A' = 0x41 = 65 β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
5. Base Conversion¶
5.1 Decimal β Binary Conversion¶
Method 1: Division (Integer part)
Convert decimal 25 to binary:
25 Γ· 2 = 12 ... remainder 1 β
12 Γ· 2 = 6 ... remainder 0 β
6 Γ· 2 = 3 ... remainder 0 β Read bottom to top
3 Γ· 2 = 1 ... remainder 1 β
1 Γ· 2 = 0 ... remainder 1 β
Result: 25ββ = 11001β
Method 2: Multiplication (Fractional part)
Convert decimal 0.625 to binary:
0.625 Γ 2 = 1.25 β integer part 1 β
0.25 Γ 2 = 0.5 β integer part 0 β
0.5 Γ 2 = 1.0 β integer part 1 β Read top to bottom
0.0 (done) β
Result: 0.625ββ = 0.101β
5.2 Binary β Decimal Conversion¶
Convert binary 110101 to decimal:
1 1 0 1 0 1
2β΅ 2β΄ 2Β³ 2Β² 2ΒΉ 2β°
32 16 8 4 2 1
= 1Γ32 + 1Γ16 + 0Γ8 + 1Γ4 + 0Γ2 + 1Γ1
= 32 + 16 + 0 + 4 + 0 + 1
= 53ββ
5.3 Binary β Hexadecimal Conversion¶
Binary β Hexadecimal: Group by 4 bits
1011 1010 0110 1111β
B A 6 F
= 0xBA6F
Hexadecimal β Binary: Expand each digit to 4 bits
0x2F5 = 0010 1111 0101β
2 F 5
5.4 Decimal β Hexadecimal Conversion¶
Decimal β Hexadecimal: Divide by 16
500 Γ· 16 = 31 ... remainder 4 β
31 Γ· 16 = 1 ... remainder 15 (F) β
1 Γ· 16 = 0 ... remainder 1 β
500ββ = 0x1F4
Hexadecimal β Decimal: Calculate place values
0x1F4 = 1Γ16Β² + 15Γ16ΒΉ + 4Γ16β°
= 256 + 240 + 4
= 500ββ
Base Conversion Summary Diagram¶
ββββββββββββββββββββ
β Decimal β
ββββββββββ¬ββββββββββ
Γ·2 β Γ·16
reverse β reverse
remainders β remainders
β
ββββββββββββββββΌβββββββββββββββ
β β β
βΌ β βΌ
βββββββββ β ββββββββββ
β BinaryββββββββββββΌβββββββββββ Hex β
βββββββββ group by 4 ββββββββββ
expand each
to 4 bits
Quick Base Conversion Reference Table¶
ββββββββββββ¬βββββββββββ¬βββββββββββ¬βββββββββββ
β Decimal β Binary β Octal β Hex β
ββββββββββββΌβββββββββββΌβββββββββββΌβββββββββββ€
β 0 β 0000 β 0 β 0 β
β 1 β 0001 β 1 β 1 β
β 2 β 0010 β 2 β 2 β
β 3 β 0011 β 3 β 3 β
β 4 β 0100 β 4 β 4 β
β 5 β 0101 β 5 β 5 β
β 6 β 0110 β 6 β 6 β
β 7 β 0111 β 7 β 7 β
β 8 β 1000 β 10 β 8 β
β 9 β 1001 β 11 β 9 β
β 10 β 1010 β 12 β A β
β 11 β 1011 β 13 β B β
β 12 β 1100 β 14 β C β
β 13 β 1101 β 15 β D β
β 14 β 1110 β 16 β E β
β 15 β 1111 β 17 β F β
β 16 β 10000 β 20 β 10 β
β 32 β 100000 β 40 β 20 β
β 64 β 1000000 β 100 β 40 β
β 128 β10000000 β 200 β 80 β
β 255 β11111111 β 377 β FF β
β 256 β100000000 β 400 β 100 β
ββββββββββββ΄βββββββββββ΄βββββββββββ΄βββββββββββ
6. Data Units¶
Basic Units¶
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Data Unit Hierarchy β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β Bit β
β βββ 0 or 1, smallest unit of information β
β β
β Nibble β
β βββ 4 bits = one hexadecimal digit β
β β
β Byte β
β βββ 8 bits = can represent one character β
β β
β Word β
β βββ Data size processed by CPU in one operation β
β (16-bit, 32-bit, 64-bit, etc.) β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Bits and Bytes¶
1 byte = 8 bits
βββββββ¬ββββββ¬ββββββ¬ββββββ¬ββββββ¬ββββββ¬ββββββ¬ββββββ
β bit7β bit6β bit5β bit4β bit3β bit2β bit1β bit0β
β(MSB)β β β β β β β(LSB)β
βββββββ΄ββββββ΄ββββββ΄ββββββ΄ββββββ΄ββββββ΄ββββββ΄ββββββ
2β· 2βΆ 2β΅ 2β΄ 2Β³ 2Β² 2ΒΉ 2β°
128 64 32 16 8 4 2 1
MSB (Most Significant Bit): Leftmost bit (highest order)
LSB (Least Significant Bit): Rightmost bit (lowest order)
Values representable in 1 byte:
- Unsigned integer: 0 ~ 255 (2βΈ = 256 values)
- Signed integer: -128 ~ 127
Word Size¶
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Word Size by System Type β
ββββββββββββββββββ¬βββββββββββ¬ββββββββββββββββββββββββββββββββ€
β System βWord Size β Value Range β
ββββββββββββββββββΌβββββββββββΌββββββββββββββββββββββββββββββββ€
β 8-bit system β 1 byte β 0 ~ 255 β
β 16-bit system β 2 bytes β 0 ~ 65,535 β
β 32-bit system β 4 bytes β 0 ~ 4,294,967,295 β
β 64-bit system β 8 bytes β 0 ~ 18,446,744,073,709,551,615β
ββββββββββββββββββ΄βββββββββββ΄ββββββββββββββββββββββββββββββββ
Large-Scale Units¶
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Storage Capacity Units β
ββββββββββββββ¬βββββββββββββββββββββ¬βββββββββββββββββββββββββββ€
β Unit β Binary Prefix β Decimal Prefix β
β β (IEC Standard) β (SI Standard) β
ββββββββββββββΌβββββββββββββββββββββΌβββββββββββββββββββββββββββ€
β Kilo (K) β 1 KiB = 2ΒΉβ° = 1,024β 1 KB = 10Β³ = 1,000 β
β Mega (M) β 1 MiB = 2Β²β° β 1 MB = 10βΆ β
β Giga (G) β 1 GiB = 2Β³β° β 1 GB = 10βΉ β
β Tera (T) β 1 TiB = 2β΄β° β 1 TB = 10ΒΉΒ² β
β Peta (P) β 1 PiB = 2β΅β° β 1 PB = 10ΒΉβ΅ β
β Exa (E) β 1 EiB = 2βΆβ° β 1 EB = 10ΒΉβΈ β
ββββββββββββββ΄βββββββββββββββββββββ΄βββββββββββββββββββββββββββ
Real capacity difference example:
- 1 TB hard drive (decimal): 1,000,000,000,000 bytes
- OS display (binary): approximately 931 GiB
Calculation: 1,000,000,000,000 Γ· 1,073,741,824 β 931 GiB
7. Concept of Complements¶
What is a Complement?¶
A complement is the value obtained by subtracting a number from
a specific reference point. In computers, complements are used
to convert subtraction into addition.
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Types of Complements β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β (r-1)'s complement: Subtract each digit from (r-1) β
β r's complement: (r-1)'s complement + 1 β
β β
β For binary: β
β - One's complement: Invert each bit (0β1) β
β - Two's complement: One's complement + 1 β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
One's Complement¶
One's complement: Invert all bits (0β1, 1β0)
Example: One's complement of 00101101 in 8 bits
Original: 0 0 1 0 1 1 0 1
β β β β β β β β
1's comp: 1 1 0 1 0 0 1 0
Verification: 45ββ β 1's complement β 210ββ
45 + 210 = 255 = 2βΈ - 1 β
Two's Complement¶
Two's complement: One's complement + 1
(or keep rightmost 1, invert left side)
Example: Two's complement of 00101101 in 8 bits
Method 1: One's complement + 1
Original: 0 0 1 0 1 1 0 1 (45)
1's comp: 1 1 0 1 0 0 1 0 (210)
+ 1
βββββββββββββββββββββββββββββββββ
2's comp: 1 1 0 1 0 0 1 1 (211)
Method 2: Keep up to first 1 from right, invert the rest
Original: 0 0 1 0 1 1 0 1
β
Keep up to here, invert left side
2's comp: 1 1 0 1 0 0 1 1
Verification: 45 + 211 = 256 = 2βΈ β
Importance of Two's Complement¶
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Why Two's Complement is Important β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β 1. Unique representation of zero β
β - One's complement: +0 (00000000), -0 (11111111) β
β - Two's complement: 0 (00000000) only β
β β
β 2. Converts subtraction to addition β
β A - B = A + (-B) = A + (two's complement of B) β
β β
β 3. Single adder can handle both addition/subtraction β
β β
β 4. Easy overflow detection β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Subtraction Using Two's Complement¶
Example: Calculate 7 - 3 using 8-bit two's complement
7 = 00000111
3 = 00000011
Two's complement of 3 = 11111101 (= -3)
0 0 0 0 0 1 1 1 (7)
+ 1 1 1 1 1 1 0 1 (-3, two's complement of 3)
βββββββββββββββββββββ
1 0 0 0 0 0 1 0 0
β
Discard carry (since 8-bit)
Result: 00000100 = 4 β
Signed Integer Range¶
n-bit two's complement range: -2^(n-1) ~ 2^(n-1) - 1
βββββββββββββ¬βββββββββββββββββββββββββββββββββββββββββββββββ
β Bit Size β Value Range β
βββββββββββββΌβββββββββββββββββββββββββββββββββββββββββββββββ€
β 4-bit β -8 ~ 7 β
β 8-bit β -128 ~ 127 β
β 16-bit β -32,768 ~ 32,767 β
β 32-bit β -2,147,483,648 ~ 2,147,483,647 β
β 64-bit β approx -9.2Γ10ΒΉβΈ ~ 9.2Γ10ΒΉβΈ β
βββββββββββββ΄βββββββββββββββββββββββββββββββββββββββββββββββ
8-bit two's complement number circle:
0 (00000000)
β
-1 (11111111) 1 (00000001)
β β
Β· Β·
Β· Β·
-128 Β· Β· 127
(10000000) (01111111)
8. Practice Problems¶
Basic Problems¶
1. Convert the following decimal numbers to binary: - (a) 42 - (b) 100 - (c) 255
2. Convert the following binary numbers to decimal: - (a) 10110 - (b) 11111111 - (c) 10000000
3. Convert the following binary numbers to hexadecimal: - (a) 11011010 - (b) 101111110000 - (c) 11111111111111111111111111111111
4. Convert the following hexadecimal numbers to binary: - (a) 0xAB - (b) 0x1234 - (c) 0xDEADBEEF
Calculation Problems¶
5. Perform the following binary additions: - (a) 1011 + 1101 - (b) 11111111 + 00000001 - (c) 10101010 + 01010101
6. In 8-bit two's complement representation: - (a) What is the binary representation of -45? - (b) What is the decimal value of 11101100? - (c) What is the result of 7 - 12?
7. Convert the following decimal fractions to binary (up to 4 decimal places): - (a) 0.5 - (b) 0.25 - (c) 0.1
Advanced Problems¶
8. What is the difference in bytes between 1 GiB and 1 GB?
9. Explain why a 32-bit system is limited to addressing a maximum of 4GB of memory.
10. Analyze the following scenario:
signed char a = 127;
a = a + 1;
// What is the value of a?
Answers
**1. Decimal β Binary** - (a) 42 = 101010 - (b) 100 = 1100100 - (c) 255 = 11111111 **2. Binary β Decimal** - (a) 10110 = 22 - (b) 11111111 = 255 - (c) 10000000 = 128 **3. Binary β Hexadecimal** - (a) 11011010 = 0xDA - (b) 101111110000 = 0xBF0 - (c) 11111111111111111111111111111111 = 0xFFFFFFFF **4. Hexadecimal β Binary** - (a) 0xAB = 10101011 - (b) 0x1234 = 0001001000110100 - (c) 0xDEADBEEF = 11011110101011011011111011101111 **5. Binary Addition** - (a) 1011 + 1101 = 11000 (11 + 13 = 24) - (b) 11111111 + 00000001 = 100000000 (result depends on overflow handling) - (c) 10101010 + 01010101 = 11111111 (170 + 85 = 255) **6. Two's Complement** - (a) -45 = 11010011 (45 = 00101101, complement = 11010011) - (b) 11101100 = -20 (MSB is 1, so negative; complement = 00010100 = 20) - (c) 7 - 12 = -5 = 11111011 **7. Decimal Fractions β Binary** - (a) 0.5 = 0.1 - (b) 0.25 = 0.01 - (c) 0.1 β 0.0001 (actually 0.0001100110011... repeating infinitely) **8.** - 1 GiB = 1,073,741,824 bytes - 1 GB = 1,000,000,000 bytes - Difference = 73,741,824 bytes β 70.3 MiB **9.** A 32-bit system uses 32-bit addresses, so it can address 2Β³Β² = 4,294,967,296 addresses. Since each address points to 1 byte, the maximum directly accessible memory is 4GB (approximately 4GiB). **10.** a = -128. The range of signed char is -128~127, so 127+1 causes overflow and wraps to -128. This is because in two's complement representation, 01111111(127) + 1 = 10000000(-128).Next Steps¶
- 03_Integer_Float_Representation.md - Detailed integer and floating-point representation
References¶
- Computer Organization and Design (Patterson & Hennessy)
- Digital Design (Morris Mano)
- Binary, Hexadecimal, Octal - CS101
- Two's Complement - Wikipedia