Number Systems
Hey there students! š Welcome to our exciting journey through the world of number systems! In this lesson, you'll discover how computers actually "think" and process information using different numerical systems. By the end of this lesson, you'll understand how to convert between binary, decimal, hexadecimal, and octal systems, perform arithmetic operations, and work with bitwise manipulation - all essential skills for any aspiring computer scientist! š
Understanding Number Systems and Their Bases
Let's start with the basics, students! A number system is simply a way of representing numbers using a specific set of symbols and rules. Think of it like different languages for expressing the same mathematical concepts! š
The most familiar system to you is the decimal system (base-10), which uses digits 0-9. But computers don't naturally work with decimal - they prefer binary (base-2), using only 0s and 1s. Why? Because computers are built from electronic switches that can only be in two states: ON (1) or OFF (0).
Here's how different bases work:
- Binary (Base-2): Uses digits 0, 1
- Octal (Base-8): Uses digits 0, 1, 2, 3, 4, 5, 6, 7
- Decimal (Base-10): Uses digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
- Hexadecimal (Base-16): Uses digits 0-9 and letters A, B, C, D, E, F
The place value in any number system follows the pattern: $base^{position}$. For example, in decimal 1234, we have:
$$1 Ć 10^3 + 2 Ć 10^2 + 3 Ć 10^1 + 4 Ć 10^0 = 1000 + 200 + 30 + 4 = 1234$$
Binary System: The Language of Computers
Binary is the foundation of all computer operations, students! Every piece of data - whether it's your favorite song, a photo, or this very text - is ultimately stored as sequences of 0s and 1s. š»
Let's look at how binary numbers work. The binary number 1101 converts to decimal as:
$$1 Ć 2^3 + 1 Ć 2^2 + 0 Ć 2^1 + 1 Ć 2^0 = 8 + 4 + 0 + 1 = 13$$
Converting Decimal to Binary:
To convert decimal 13 to binary, we repeatedly divide by 2 and track remainders:
- 13 Ć· 2 = 6 remainder 1
- 6 Ć· 2 = 3 remainder 0
- 3 Ć· 2 = 1 remainder 1
- 1 Ć· 2 = 0 remainder 1
Reading remainders from bottom to top: 1101
Binary Arithmetic:
Adding binary numbers follows similar rules to decimal, but we only have 0 and 1:
$- 0 + 0 = 0$
$- 0 + 1 = 1 $
$- 1 + 0 = 1$
- 1 + 1 = 10 (carry the 1)
For example: 1011 + 1101 = 11000
Hexadecimal and Octal Systems
Hexadecimal (Hex) is incredibly useful in computing because it provides a compact way to represent binary data. Each hex digit represents exactly 4 binary digits! šÆ
Here's the hex-to-decimal mapping:
- 0-9 represent values 0-9
- A=10, B=11, C=12, D=13, E=14, F=15
Converting hex 2A3 to decimal:
$$2 Ć 16^2 + 10 Ć 16^1 + 3 Ć 16^0 = 512 + 160 + 3 = 675$$
Octal (base-8) was historically important and each octal digit represents exactly 3 binary digits. Converting octal 347 to decimal:
$$3 Ć 8^2 + 4 Ć 8^1 + 7 Ć 8^0 = 192 + 32 + 7 = 231$$
Quick Conversion Tips:
- Binary to Hex: Group binary digits in sets of 4 from right to left
- Binary to Octal: Group binary digits in sets of 3 from right to left
- Example: 11010110ā = 1101|0110 = D6āā = 011|010|110 = 326ā
Logic Operations and Boolean Algebra
Now let's explore the logical operations that make computers so powerful, students! These operations work on individual bits and form the basis of all computer decision-making. š§
Basic Logic Operations:
AND Operation (&): Returns 1 only when both inputs are 1
- 1 AND 1 = 1
- 1 AND 0 = 0
- 0 AND 1 = 0
- 0 AND 0 = 0
OR Operation (|): Returns 1 when at least one input is 1
$- 1 OR 1 = 1$
$- 1 OR 0 = 1$
$- 0 OR 1 = 1$
$- 0 OR 0 = 0$
NOT Operation (~): Flips the bit
$- NOT 1 = 0$
$- NOT 0 = 1$
XOR Operation (ā): Returns 1 when inputs are different
$- 1 XOR 1 = 0$
$- 1 XOR 0 = 1$
$- 0 XOR 1 = 1$
$- 0 XOR 0 = 0$
Bitwise Manipulation Techniques
Bitwise operations are incredibly powerful tools for efficient programming, students! They operate directly on the binary representation of numbers, making them extremely fast. ā”
Common Bitwise Operations:
Left Shift (<<): Shifts bits to the left, effectively multiplying by powers of 2
- 5 << 1 = 10 (binary: 101 becomes 1010)
- 5 << 2 = 20 (binary: 101 becomes 10100)
Right Shift (>>): Shifts bits to the right, effectively dividing by powers of 2
- 20 >> 1 = 10 (binary: 10100 becomes 1010)
- 20 >> 2 = 5 (binary: 10100 becomes 101)
Practical Applications:
- Checking if a number is even:
n & 1 == 0 - Setting a specific bit:
n | (1 << position) - Clearing a specific bit:
n & ~(1 << position) - Toggling a specific bit:
n ^ (1 << position)
Real-world Example: In graphics programming, colors are often represented as 32-bit integers where each byte represents red, green, blue, and alpha channels. Bitwise operations allow programmers to efficiently extract and modify individual color components!
Practical Applications in Computing
Understanding number systems isn't just academic - it's essential for real-world computing, students! š
Memory Addresses: Computer memory addresses are typically expressed in hexadecimal because it's much more readable than binary. For example, the memory address 0x7FFF0000 is much cleaner than 01111111111111110000000000000000!
File Permissions: Unix/Linux systems use octal notation for file permissions. The permission 755 means:
- 7 (111ā): Read, write, execute for owner
- 5 (101ā): Read and execute for group
- 5 (101ā): Read and execute for others
Network Programming: IP addresses, subnet masks, and network calculations heavily rely on binary operations and understanding of different number systems.
Cryptography: Many encryption algorithms use XOR operations and bitwise manipulation for scrambling and unscrambling data securely.
Conclusion
Fantastic work, students! š You've now mastered the fundamental number systems that power all of computing. You've learned how to convert between binary, decimal, hexadecimal, and octal systems, perform arithmetic operations in different bases, and use powerful bitwise manipulation techniques. These skills form the foundation for understanding how computers process information at the most basic level, and you'll use these concepts throughout your computer science journey - from low-level programming to algorithm optimization and beyond!
Study Notes
⢠Number System Bases: Binary (2), Octal (8), Decimal (10), Hexadecimal (16)
⢠Place Value Formula: $digit à base^{position}$
⢠Binary to Decimal: Sum of $bit à 2^{position}$ for each position
⢠Decimal to Binary: Repeatedly divide by 2, track remainders
⢠Hex Digits: 0-9, A(10), B(11), C(12), D(13), E(14), F(15)
⢠Binary Grouping: 4 bits = 1 hex digit, 3 bits = 1 octal digit
⢠Logic Operations: AND (&), OR (|), NOT (~), XOR (ā)
⢠AND Truth: Only 1 & 1 = 1
⢠OR Truth: Any input of 1 gives 1
⢠XOR Truth: Different inputs give 1
⢠Left Shift: Multiplies by $2^n$ where n is shift amount
⢠Right Shift: Divides by $2^n$ where n is shift amount
⢠Even Check: n & 1 == 0
⢠Set Bit: n | (1 << position)
⢠Clear Bit: n & ~(1 << position)
⢠Toggle Bit: n ^ (1 << position)
