Number Systems and Binary
Learning Objectives
By the end of this reading, you will be able to:
- Understand positional number systems and their mathematical foundations
- Convert between binary, decimal, hexadecimal, and octal number systems
- Perform arithmetic operations in binary
- Represent negative numbers using two's complement
- Understand the relationship between binary and computer memory
- Apply number systems to solve real-world computing problems
Introduction
In everyday life, we use the decimal (base-10) number system, which has ten digits (0-9). Computers, however, operate using the binary (base-2) system, which has only two digits (0 and 1). This is because digital circuits work with two voltage states: high (1) and low (0).
Understanding number systems is fundamental to computer science because:
- All data in computers is ultimately stored as binary numbers
- Hexadecimal and octal provide convenient shorthand for binary
- Number systems are essential for understanding memory addresses, colors, permissions, and more
Positional Number Systems
Understanding Place Value
In a positional number system, the position of each digit determines its value. The general formula for a number in base b is:
N = d_n × b^n + d_(n-1) × b^(n-1) + ... + d_1 × b^1 + d_0 × b^0
Where:
d_iis the digit at position ibis the base of the number systemnis the position (counting from 0 on the right)
Example: Decimal (Base-10)
The number 3,247 in decimal:
3247₁₀ = 3×10³ + 2×10² + 4×10¹ + 7×10⁰
= 3×1000 + 2×100 + 4×10 + 7×1
= 3000 + 200 + 40 + 7
= 3247
Binary (Base-2)
Binary uses only two digits: 0 and 1. Each binary digit is called a bit (binary digit).
Binary Place Values
Position: 7 6 5 4 3 2 1 0
─────────────────────────────────────────
Power: 2⁷ 2⁶ 2⁵ 2⁴ 2³ 2² 2¹ 2⁰
─────────────────────────────────────────
Value: 128 64 32 16 8 4 2 1
Example: Binary to Decimal
Convert 10110101₂ to decimal:
1 0 1 1 0 1 0 1
↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
128 + 0 + 32 + 16 + 0 + 4 + 0 + 1 = 181₁₀
Decimal to Binary Conversion
Method 1: Division by 2
To convert decimal to binary, repeatedly divide by 2 and record remainders:
Convert 181₁₀ to binary:
181 ÷ 2 = 90 remainder 1 ← LSB (Least Significant Bit)
90 ÷ 2 = 45 remainder 0
45 ÷ 2 = 22 remainder 1
22 ÷ 2 = 11 remainder 0
11 ÷ 2 = 5 remainder 1
5 ÷ 2 = 2 remainder 1
2 ÷ 2 = 1 remainder 0
1 ÷ 2 = 0 remainder 1 ← MSB (Most Significant Bit)
Reading from bottom to top: 10110101₂
Method 2: Subtraction Method
Find the largest power of 2 ≤ the number, subtract it, and repeat:
Convert 181₁₀ to binary:
181 - 128 (2⁷) = 53 → bit 7 = 1
53 - 64 (2⁶) = NO → bit 6 = 0
53 - 32 (2⁵) = 21 → bit 5 = 1
21 - 16 (2⁴) = 5 → bit 4 = 1
5 - 8 (2³) = NO → bit 3 = 0
5 - 4 (2²) = 1 → bit 2 = 1
1 - 2 (2¹) = NO → bit 1 = 0
1 - 1 (2⁰) = 0 → bit 0 = 1
Result: 10110101₂
Binary Arithmetic
Binary Addition
Rules:
0 + 0 = 0
0 + 1 = 1
1 + 0 = 1
1 + 1 = 10 (0 with carry of 1)
Example:
1 0 1 1 0 (22₁₀)
+ 0 1 1 0 1 (13₁₀)
───────────
1 0 0 0 1 1 (35₁₀)
Carries: ¹ ¹ ¹
Binary Subtraction
Rules:
0 - 0 = 0
1 - 0 = 1
1 - 1 = 0
0 - 1 = 1 (with borrow)
Example:
1 0 1 1 0 (22₁₀)
- 0 0 1 0 1 (5₁₀)
───────────
1 0 0 0 1 (17₁₀)
Binary Multiplication
Similar to decimal multiplication, but simpler:
1 0 1 1 (11₁₀)
× 1 0 1 (5₁₀)
───────────
1 0 1 1 (11 × 1)
0 0 0 0 (11 × 0, shifted)
1 0 1 1 (11 × 1, shifted)
───────────
1 1 0 1 1 1 (55₁₀)
Hexadecimal (Base-16)
Hexadecimal uses 16 digits: 0-9 and A-F (where A=10, B=11, C=12, D=13, E=14, F=15).
Why Hexadecimal?
- One hex digit represents exactly 4 binary bits (a nibble)
- Compact representation of binary data
- Commonly used for memory addresses, colors, and more
Hex Place Values
Position: 3 2 1 0
─────────────────────────
Power: 16³ 16² 16¹ 16⁰
─────────────────────────
Value: 4096 256 16 1
Binary-Hexadecimal Conversion
Binary to Hex: Group bits into fours (from right), convert each group:
Binary: 1010 1110 1101 0011
Hex: A E D 3
Result: AED3₁₆
Hex to Binary: Convert each hex digit to 4 binary bits:
Hex: 2 F A 8
Binary: 0010 1111 1010 1000
Result: 00101111101010002
Hex-Decimal Conversion
Hex to Decimal:
3FA₁₆ = 3×16² + 15×16¹ + 10×16⁰
= 3×256 + 15×16 + 10×1
= 768 + 240 + 10
= 1018₁₀
Decimal to Hex: Divide by 16 repeatedly:
Convert 1018₁₀ to hex:
1018 ÷ 16 = 63 remainder 10 (A) ← LSD
63 ÷ 16 = 3 remainder 15 (F)
3 ÷ 16 = 0 remainder 3 ← MSD
Result: 3FA₁₆
Octal (Base-8)
Octal uses 8 digits: 0-7. One octal digit represents exactly 3 binary bits.
Binary-Octal Conversion
Binary to Octal: Group bits into threes:
Binary: 101 110 111 010
Octal: 5 6 7 2
Result: 5672₈
Octal to Decimal:
5672₈ = 5×8³ + 6×8² + 7×8¹ + 2×8⁰
= 5×512 + 6×64 + 7×8 + 2×1
= 2560 + 384 + 56 + 2
= 3002₁₀
Quick Reference Table
┌─────────┬─────────┬──────┬───────┐
│ Decimal │ Binary │ Hex │ Octal │
├─────────┼─────────┼──────┼───────┤
│ 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 │ 8 │ 10 │
│ 9 │ 1001 │ 9 │ 11 │
│ 10 │ 1010 │ A │ 12 │
│ 11 │ 1011 │ B │ 13 │
│ 12 │ 1100 │ C │ 14 │
│ 13 │ 1101 │ D │ 15 │
│ 14 │ 1110 │ E │ 16 │
│ 15 │ 1111 │ F │ 17 │
└─────────┴─────────┴──────┴───────┘
Representing Negative Numbers
Sign-Magnitude
The leftmost bit represents the sign (0 = positive, 1 = negative):
8-bit examples:
01010110 = +86₁₀
11010110 = -86₁₀
Problems:
- Two representations of zero: 00000000 and 10000000
- Complicated arithmetic circuits
One's Complement
Negative numbers are formed by flipping all bits:
01010110 = +86₁₀
10101001 = -86₁₀ (flip all bits)
Problems:
- Still has two zeros: 00000000 and 11111111
- Addition requires end-around carry
Two's Complement (Standard Method)
Formation: Flip all bits and add 1
+86₁₀ = 01010110
Step 1: Flip bits → 10101001
Step 2: Add 1 → 10101010
-86₁₀ = 10101010
Advantages:
- Only one representation of zero
- Addition and subtraction use the same circuit
- Most significant bit indicates sign (0=positive, 1=negative)
Two's Complement Range
For n bits, the range is: -2^(n-1) to 2^(n-1) - 1
8-bit range: -128 to +127
16-bit range: -32,768 to +32,767
32-bit range: -2,147,483,648 to +2,147,483,647
Two's Complement Arithmetic
Addition:
01010110 (+86)
+ 11110110 (-10)
──────────
101001100
^^^^^^^^
discard overflow
Result: 01001100 = +76₁₀ ✓
Subtraction: Convert to addition of negative
86 - 10 = 86 + (-10) [Use addition above]
Converting Two's Complement to Decimal
If MSB = 0 (positive): Convert normally
If MSB = 1 (negative):
- Flip all bits
- Add 1
- Convert to decimal
- Make negative
Example: 11010110
Step 1: Flip → 00101001
Step 2: Add 1 → 00101010
Step 3: Convert → 42₁₀
Step 4: Negate → -42₁₀
Binary in Computing
Bits, Bytes, and Words
1 bit = 1 binary digit
1 nibble = 4 bits (1 hex digit)
1 byte = 8 bits (2 hex digits)
1 word = Architecture-dependent (16, 32, or 64 bits)
Memory Sizes
1 KB (Kilobyte) = 1,024 bytes = 2¹⁰ bytes
1 MB (Megabyte) = 1,024 KB = 2²⁰ bytes
1 GB (Gigabyte) = 1,024 MB = 2³⁰ bytes
1 TB (Terabyte) = 1,024 GB = 2⁴⁰ bytes
Note: Storage manufacturers often use decimal (1000-based) instead of binary (1024-based), causing discrepancies.
Hexadecimal in Practice
Memory Addresses:
0x00400000 ← Program code
0x7FFFFFFF ← Stack
0xFFFFFFFF ← Maximum 32-bit address
RGB Colors:
#FF0000 = Red (255, 0, 0)
#00FF00 = Green (0, 255, 0)
#0000FF = Blue (0, 0, 255)
#FFFFFF = White (255, 255, 255)
File Permissions (Unix):
0644 (octal) = rw-r--r--
0755 (octal) = rwxr-xr-x
Practical Examples
Example 1: IP Address to Binary
Convert 192.168.1.1 to binary:
192 → 11000000
168 → 10101000
1 → 00000001
1 → 00000001
Result: 11000000.10101000.00000001.00000001
Example 2: Subnet Mask
Subnet mask 255.255.255.0 in binary:
255 → 11111111
255 → 11111111
255 → 11111111
0 → 00000000
Result: 11111111.11111111.11111111.00000000
(24 ones, indicating a /24 network)
Example 3: Color Code
What color is #2A5F7B?
2A → 00101010 = 42₁₀ (Red component)
5F → 01011111 = 95₁₀ (Green component)
7B → 01111011 = 123₁₀ (Blue component)
RGB(42, 95, 123) = Dark blue-green
Programming Examples
Python: Number System Conversions
# Decimal to other bases
decimal = 255
binary = bin(decimal) # '0b11111111'
hexadecimal = hex(decimal) # '0xff'
octal = oct(decimal) # '0o377'
# Remove prefix for clean output
binary_clean = bin(decimal)[2:] # '11111111'
hex_clean = hex(decimal)[2:] # 'ff'
octal_clean = oct(decimal)[2:] # '377'
# Other bases to decimal
binary_str = '11111111'
hex_str = 'FF'
octal_str = '377'
from_binary = int(binary_str, 2) # 255
from_hex = int(hex_str, 16) # 255
from_octal = int(octal_str, 8) # 255
Python: Two's Complement
def twos_complement(value, bits):
"""Convert a signed integer to two's complement representation"""
if value < 0:
# Calculate two's complement for negative numbers
value = (1 << bits) + value
return value
def from_twos_complement(value, bits):
"""Convert from two's complement to signed integer"""
# Check if sign bit is set
if value & (1 << (bits - 1)):
# Negative number: subtract 2^bits
return value - (1 << bits)
return value
# Examples
print(twos_complement(-86, 8)) # 170 (0xAA)
print(from_twos_complement(170, 8)) # -86
C: Bitwise Operations
#include <stdio.h>
void print_binary(unsigned char n) {
for (int i = 7; i >= 0; i--) {
printf("%d", (n >> i) & 1);
}
printf("\n");
}
int main() {
unsigned char a = 0b10110101; // 181
unsigned char b = 0b01011010; // 90
printf("a = "); print_binary(a);
printf("b = "); print_binary(b);
printf("a & b = "); print_binary(a & b); // AND
printf("a | b = "); print_binary(a | b); // OR
printf("a ^ b = "); print_binary(a ^ b); // XOR
printf("~a = "); print_binary(~a); // NOT
printf("a << 2 = "); print_binary(a << 2); // Left shift
printf("a >> 2 = "); print_binary(a >> 2); // Right shift
return 0;
}
Exercises
Basic Exercises
Binary-Decimal Conversion
- Convert 11010110₂ to decimal
- Convert 203₁₀ to binary
- Convert 11111111₂ to decimal
Hexadecimal Conversion
- Convert 0x3C to binary
- Convert 0xFF to decimal
- Convert 156₁₀ to hexadecimal
Binary Arithmetic
- Add: 1011₂ + 1101₂
- Subtract: 10110₂ - 1010₂
- Multiply: 101₂ × 11₂
Intermediate Exercises
Two's Complement
- Represent -45 in 8-bit two's complement
- What decimal number does 11001100 (8-bit two's complement) represent?
- Add: 01010101 + 11011010 (8-bit two's complement)
Mixed Conversions
- Convert 0x2A from hex → binary → decimal → octal
- What is 100₈ in binary, decimal, and hex?
- Convert 377₈ to hexadecimal
Practical Applications
- Convert IP address 10.0.0.255 to binary
- What color is #FF6B35? (convert to RGB decimal)
- What is the range of a 12-bit two's complement number?
Advanced Exercises
Arithmetic Challenges
- Perform 8-bit two's complement: 45 - 67
- Detect overflow: Add 01111111 + 00000001 (8-bit signed)
- Multiply 1101₂ × 1011₂ using binary multiplication
Bitwise Operations
- What is 0xA5 & 0x3C?
- Calculate: 10110100 XOR 11001010
- How can you use AND to check if a number is even?
Problem Solving
- How many different colors can be represented in 24-bit RGB?
- If a system uses 16-bit addressing, what is the maximum memory addressable?
- Design a method to swap two numbers using XOR without a temp variable
Algorithm Development
- Write pseudocode to convert decimal to any base (2-16)
- Develop an algorithm to add two binary numbers of arbitrary length
- Create a method to determine if a binary number is a power of 2
Summary
In this reading, we explored the fundamental number systems used in computing:
Key Concepts:
- Positional notation allows us to represent numbers in different bases
- Binary (base-2) is the fundamental number system for computers
- Hexadecimal (base-16) provides compact representation of binary data
- Octal (base-8) is occasionally used for file permissions and other systems
- Two's complement is the standard method for representing signed integers
Important Skills:
- Converting between number systems (binary, decimal, hex, octal)
- Performing arithmetic in binary
- Understanding two's complement representation
- Recognizing hexadecimal in real-world applications
Why This Matters:
- All computer data is ultimately binary
- Understanding number systems helps you debug, optimize, and understand low-level operations
- Essential foundation for computer architecture, assembly programming, and systems programming
Further Reading
- "Code: The Hidden Language of Computer Hardware and Software" by Charles Petzold
- IEEE 754 Floating-Point Standard (for representing real numbers)
- Boolean algebra and bit manipulation techniques
Next Steps
Now that you understand how computers represent numbers, you're ready to learn how they process them using logic gates and circuits.
Next Reading: 02-logic-gates.md - Logic Gates and Circuits
Module 5: Computer Architecture | Reading 1 of 5