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

  1. Understanding Number Systems
  2. Binary (Base-2)
  3. Octal (Base-8)
  4. Hexadecimal (Base-16)
  5. Base Conversion
  6. Data Units
  7. Concept of Complements
  8. 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


References

to navigate between lessons