Coming Soon
This lesson is currently being developed
Numeral systems (decimal binary hexadecimal and octal)
Learn about different number bases: binary, octal, decimal, hexadecimal.
What to Expect
Comprehensive explanations with practical examples
Interactive coding exercises to practice concepts
Knowledge quiz to test your understanding
Step-by-step guidance for beginners
Development Status
Content is being carefully crafted to provide the best learning experience
Preview
Early Preview Content
This content is still being developed and may change before publication.
5.3 — Numeral systems (decimal, binary, hexadecimal, and octal)
In this lesson, you'll learn about different numeral systems used in programming, how to work with them in C++, and understand their practical applications.
What are numeral systems?
A numeral system (or number system) is a way to represent numbers using digits and positional notation. Different numeral systems use different bases, which determine how many unique digits are used and how positions are valued.
Think of numeral systems like different languages for numbers - they all represent the same values, but use different symbols and rules.
The four main numeral systems in programming
- Decimal (base 10) - Uses digits 0-9
- Binary (base 2) - Uses digits 0-1
- Octal (base 8) - Uses digits 0-7
- Hexadecimal (base 16) - Uses digits 0-9 and letters A-F
Decimal numeral system (base 10)
Decimal is the numeral system we use in everyday life. It uses 10 digits (0-9) and each position represents a power of 10.
Understanding decimal positions
#include <iostream>
int main()
{
// Decimal number breakdown
int number = 1234;
std::cout << "Decimal number: " << number << std::endl;
std::cout << "Breakdown:" << std::endl;
std::cout << "1 × 10³ = 1 × 1000 = 1000" << std::endl;
std::cout << "2 × 10² = 2 × 100 = 200" << std::endl;
std::cout << "3 × 10¹ = 3 × 10 = 30" << std::endl;
std::cout << "4 × 10⁰ = 4 × 1 = 4" << std::endl;
std::cout << "Total: 1000 + 200 + 30 + 4 = " << number << std::endl;
return 0;
}
Output:
Decimal number: 1234
Breakdown:
1 × 10³ = 1 × 1000 = 1000
2 × 10² = 2 × 100 = 200
3 × 10¹ = 3 × 10 = 30
4 × 10⁰ = 4 × 1 = 4
Total: 1000 + 200 + 30 + 4 = 1234
Binary numeral system (base 2)
Binary uses only two digits: 0 and 1. Each position represents a power of 2. This is the numeral system computers use internally.
Binary literals in C++
#include <iostream>
int main()
{
// Binary literals (C++14 and later) - prefix with 0b or 0B
int binaryNumber = 0b1010;
int anotherBinary = 0B11110000;
std::cout << "Binary 0b1010 = " << binaryNumber << " in decimal" << std::endl;
std::cout << "Binary 0b11110000 = " << anotherBinary << " in decimal" << std::endl;
// Using digit separators for readability
int longBinary = 0b1100'1010'1111'0000;
std::cout << "Binary 0b1100'1010'1111'0000 = " << longBinary << " in decimal" << std::endl;
return 0;
}
Output:
Binary 0b1010 = 10 in decimal
Binary 0b11110000 = 240 in decimal
Binary 0b1100'1010'1111'0000 = 51952 in decimal
Understanding binary positions
#include <iostream>
int main()
{
// Binary number 1011 breakdown
int binary = 0b1011;
std::cout << "Binary number: 0b1011" << std::endl;
std::cout << "Decimal equivalent: " << binary << std::endl;
std::cout << "Breakdown:" << std::endl;
std::cout << "1 × 2³ = 1 × 8 = 8" << std::endl;
std::cout << "0 × 2² = 0 × 4 = 0" << std::endl;
std::cout << "1 × 2¹ = 1 × 2 = 2" << std::endl;
std::cout << "1 × 2⁰ = 1 × 1 = 1" << std::endl;
std::cout << "Total: 8 + 0 + 2 + 1 = " << binary << std::endl;
return 0;
}
Output:
Binary number: 0b1011
Decimal equivalent: 11
Breakdown:
1 × 2³ = 1 × 8 = 8
0 × 2² = 0 × 4 = 0
1 × 2¹ = 1 × 2 = 2
1 × 2⁰ = 1 × 1 = 1
Total: 8 + 0 + 2 + 1 = 11
Common binary values
#include <iostream>
int main()
{
// Common binary values
std::cout << "Common binary patterns:" << std::endl;
std::cout << "0b0000 = " << 0b0000 << std::endl; // 0
std::cout << "0b0001 = " << 0b0001 << std::endl; // 1
std::cout << "0b0010 = " << 0b0010 << std::endl; // 2
std::cout << "0b0100 = " << 0b0100 << std::endl; // 4
std::cout << "0b1000 = " << 0b1000 << std::endl; // 8
std::cout << "0b1111 = " << 0b1111 << std::endl; // 15
std::cout << "0b10000 = " << 0b10000 << std::endl; // 16
std::cout << "0b11111111 = " << 0b11111111 << std::endl; // 255
return 0;
}
Output:
Common binary patterns:
0b0000 = 0
0b0001 = 1
0b0010 = 2
0b0100 = 4
0b1000 = 8
0b1111 = 15
0b10000 = 16
0b11111111 = 255
Octal numeral system (base 8)
Octal uses eight digits (0-7) and each position represents a power of 8. It's less common today but still used in some contexts.
Octal literals in C++
#include <iostream>
int main()
{
// Octal literals - prefix with 0
int octalNumber = 012; // 12 in octal
int anotherOctal = 0755; // 755 in octal (common file permission)
int zeroOctal = 0; // 0 is the same in all bases
std::cout << "Octal 012 = " << octalNumber << " in decimal" << std::endl;
std::cout << "Octal 0755 = " << anotherOctal << " in decimal" << std::endl;
std::cout << "Octal 0 = " << zeroOctal << " in decimal" << std::endl;
return 0;
}
Output:
Octal 012 = 10 in decimal
Octal 0755 = 493 in decimal
Octal 0 = 0 in decimal
Understanding octal positions
#include <iostream>
int main()
{
// Octal number 347 breakdown
int octal = 0347;
std::cout << "Octal number: 0347" << std::endl;
std::cout << "Decimal equivalent: " << octal << std::endl;
std::cout << "Breakdown:" << std::endl;
std::cout << "3 × 8² = 3 × 64 = 192" << std::endl;
std::cout << "4 × 8¹ = 4 × 8 = 32" << std::endl;
std::cout << "7 × 8⁰ = 7 × 1 = 7" << std::endl;
std::cout << "Total: 192 + 32 + 7 = " << octal << std::endl;
return 0;
}
Output:
Octal number: 0347
Decimal equivalent: 231
Breakdown:
3 × 8² = 3 × 64 = 192
4 × 8¹ = 4 × 8 = 32
7 × 8⁰ = 7 × 1 = 7
Total: 192 + 32 + 7 = 231
Hexadecimal numeral system (base 16)
Hexadecimal uses sixteen digits (0-9, A-F) where A=10, B=11, C=12, D=13, E=14, F=15. Each position represents a power of 16.
Hexadecimal literals in C++
#include <iostream>
int main()
{
// Hexadecimal literals - prefix with 0x or 0X
int hexNumber = 0x1A; // 1A in hex
int anotherHex = 0xFF; // FF in hex (255 decimal)
int colorValue = 0x00FF00; // Green color in RGB
int largeHex = 0xDEADBEEF; // Common test value
std::cout << "Hex 0x1A = " << hexNumber << " in decimal" << std::endl;
std::cout << "Hex 0xFF = " << anotherHex << " in decimal" << std::endl;
std::cout << "Hex 0x00FF00 = " << colorValue << " in decimal" << std::endl;
std::cout << "Hex 0xDEADBEEF = " << largeHex << " in decimal" << std::endl;
return 0;
}
Output:
Hex 0x1A = 26 in decimal
Hex 0xFF = 255 in decimal
Hex 0x00FF00 = 65280 in decimal
Hex 0xDEADBEEF = -559038737 in decimal
Understanding hexadecimal positions
#include <iostream>
int main()
{
// Hexadecimal number 2AF breakdown
int hex = 0x2AF;
std::cout << "Hexadecimal number: 0x2AF" << std::endl;
std::cout << "Decimal equivalent: " << hex << std::endl;
std::cout << "Breakdown:" << std::endl;
std::cout << "2 × 16² = 2 × 256 = 512" << std::endl;
std::cout << "A × 16¹ = 10 × 16 = 160 (A = 10)" << std::endl;
std::cout << "F × 16⁰ = 15 × 1 = 15 (F = 15)" << std::endl;
std::cout << "Total: 512 + 160 + 15 = " << hex << std::endl;
return 0;
}
Output:
Hexadecimal number: 0x2AF
Decimal equivalent: 687
Breakdown:
2 × 16² = 2 × 256 = 512
A × 16¹ = 10 × 16 = 160 (A = 10)
F × 16⁰ = 15 × 1 = 15 (F = 15)
Total: 512 + 160 + 15 = 687
Hexadecimal digit reference
#include <iostream>
int main()
{
std::cout << "Hexadecimal digit values:" << std::endl;
std::cout << "0 = 0, 1 = 1, 2 = 2, 3 = 3" << std::endl;
std::cout << "4 = 4, 5 = 5, 6 = 6, 7 = 7" << std::endl;
std::cout << "8 = 8, 9 = 9, A = 10, B = 11" << std::endl;
std::cout << "C = 12, D = 13, E = 14, F = 15" << std::endl;
std::cout << "\nHex examples:" << std::endl;
std::cout << "0x0 = " << 0x0 << std::endl;
std::cout << "0xA = " << 0xA << std::endl;
std::cout << "0xF = " << 0xF << std::endl;
std::cout << "0x10 = " << 0x10 << std::endl;
std::cout << "0x1F = " << 0x1F << std::endl;
std::cout << "0x100 = " << 0x100 << std::endl;
return 0;
}
Output:
Hexadecimal digit values:
0 = 0, 1 = 1, 2 = 2, 3 = 3
4 = 4, 5 = 5, 6 = 6, 7 = 7
8 = 8, 9 = 9, A = 10, B = 11
C = 12, D = 13, E = 14, F = 15
Hex examples:
0x0 = 0
0xA = 10
0xF = 15
0x10 = 16
0x1F = 31
0x100 = 256
Comparing all numeral systems
Here's how the same values look in different numeral systems:
#include <iostream>
int main()
{
// Same values in different numeral systems
int decimal = 42;
int binary = 0b101010;
int octal = 052;
int hex = 0x2A;
std::cout << "All representing the value 42:" << std::endl;
std::cout << "Decimal: " << decimal << std::endl;
std::cout << "Binary: 0b101010 = " << binary << std::endl;
std::cout << "Octal: 052 = " << octal << std::endl;
std::cout << "Hex: 0x2A = " << hex << std::endl;
// Verify they're all equal
if (decimal == binary && binary == octal && octal == hex)
{
std::cout << "\nAll values are equal!" << std::endl;
}
return 0;
}
Output:
All representing the value 42:
Decimal: 42
Binary: 0b101010 = 42
Octal: 052 = 42
Hex: 0x2A = 42
All values are equal!
Practical applications
1. Colors in programming
#include <iostream>
int main()
{
// RGB color values are commonly expressed in hexadecimal
int red = 0xFF0000; // Pure red
int green = 0x00FF00; // Pure green
int blue = 0x0000FF; // Pure blue
int white = 0xFFFFFF; // White (all colors max)
int black = 0x000000; // Black (no color)
std::cout << "Color values (RGB hex to decimal):" << std::endl;
std::cout << "Red (0xFF0000): " << red << std::endl;
std::cout << "Green (0x00FF00): " << green << std::endl;
std::cout << "Blue (0x0000FF): " << blue << std::endl;
std::cout << "White (0xFFFFFF): " << white << std::endl;
std::cout << "Black (0x000000): " << black << std::endl;
return 0;
}
Output:
Color values (RGB hex to decimal):
Red (0xFF0000): 16711680
Green (0x00FF00): 65280
Blue (0x0000FF): 255
White (0xFFFFFF): 16777215
Black (0x000000): 0
2. Memory addresses and bit operations
#include <iostream>
int main()
{
// Memory addresses are often shown in hex
int value = 42;
std::cout << "Variable value: " << value << std::endl;
std::cout << "Memory address (hex): " << &value << std::endl;
// Bit manipulation uses binary thinking
int flags = 0b1010; // Binary flags
int mask = 0b0010; // Mask to check second bit
std::cout << "\nBit manipulation:" << std::endl;
std::cout << "Flags: 0b1010 = " << flags << std::endl;
std::cout << "Mask: 0b0010 = " << mask << std::endl;
std::cout << "Flags & Mask: " << (flags & mask) << std::endl;
return 0;
}
3. File permissions (octal)
#include <iostream>
int main()
{
// Unix/Linux file permissions often use octal
int readWriteExecute = 0777; // rwxrwxrwx
int readWrite = 0644; // rw-r--r--
int readOnly = 0444; // r--r--r--
std::cout << "File permissions (octal to decimal):" << std::endl;
std::cout << "0777 (rwxrwxrwx): " << readWriteExecute << std::endl;
std::cout << "0644 (rw-r--r--): " << readWrite << std::endl;
std::cout << "0444 (r--r--r--): " << readOnly << std::endl;
return 0;
}
Output:
File permissions (octal to decimal):
0777 (rwxrwxrwx): 511
0644 (rw-r--r--): 420
0444 (r--r--r--): 292
Output formatting for different bases
C++ provides ways to output numbers in different bases:
#include <iostream>
#include <iomanip>
int main()
{
int number = 255;
std::cout << "Number 255 in different bases:" << std::endl;
std::cout << "Decimal: " << std::dec << number << std::endl;
std::cout << "Hexadecimal: " << std::hex << number << std::endl;
std::cout << "Octal: " << std::oct << number << std::endl;
// With prefixes and uppercase
std::cout << std::dec << "\nWith prefixes:" << std::endl;
std::cout << "Decimal: " << number << std::endl;
std::cout << "Hex (0x): 0x" << std::hex << number << std::endl;
std::cout << "Hex (uppercase): 0x" << std::uppercase << std::hex << number << std::endl;
std::cout << "Octal (0): 0" << std::oct << number << std::endl;
return 0;
}
Output:
Number 255 in different bases:
Decimal: 255
Hexadecimal: ff
Octal: 377
With prefixes:
Decimal: 255
Hex (0x): 0xff
Hex (uppercase): 0xFF
Octal (0): 0377
Common conversion patterns
Powers of 2 (important in binary)
#include <iostream>
int main()
{
std::cout << "Powers of 2 (binary patterns):" << std::endl;
std::cout << "2⁰ = " << (1 << 0) << " = 0b" << std::oct << (1 << 0) << std::endl; // Reset to decimal
for (int i = 0; i <= 8; ++i)
{
int power = (1 << i); // 2^i
std::cout << "2^" << i << " = " << std::dec << power;
std::cout << " = 0x" << std::hex << power << std::endl;
}
return 0;
}
Best practices for numeral systems
- Use appropriate prefixes to make the base clear
- Use digit separators for readability in long numbers
- Choose the right base for the context:
- Binary: For bit flags and low-level operations
- Octal: For file permissions (mainly on Unix systems)
- Hexadecimal: For colors, memory addresses, and byte values
- Decimal: For most general-purpose calculations
#include <iostream>
int main()
{
// Good practices
int colorRed = 0xFF0000; // Hex for colors
int filePerms = 0644; // Octal for permissions
int bitFlags = 0b1101'0010; // Binary for bit operations
int normalCount = 1'234'567; // Decimal with separators
std::cout << "Best practices examples:" << std::endl;
std::cout << "Color (hex): 0xFF0000 = " << colorRed << std::endl;
std::cout << "Permissions (octal): 0644 = " << filePerms << std::endl;
std::cout << "Bit flags (binary): 0b11010010 = " << bitFlags << std::endl;
std::cout << "Count (decimal): 1'234'567 = " << normalCount << std::endl;
return 0;
}
Output:
Best practices examples:
Color (hex): 0xFF0000 = 16711680
Permissions (octal): 0644 = 420
Bit flags (binary): 0b11010010 = 210
Count (decimal): 1'234'567 = 1234567
Summary
Understanding numeral systems is crucial for programming:
- Decimal (base 10): Standard number system, no prefix needed
- Binary (base 2):
0b
prefix, uses digits 0-1, essential for bit operations - Octal (base 8):
0
prefix, uses digits 0-7, common in file permissions - Hexadecimal (base 16):
0x
prefix, uses 0-9 and A-F, common for colors and memory
Key takeaways:
- All systems represent the same values, just using different notation
- Choose the appropriate system for your use case
- Use digit separators for readability in long numbers
- Understand the relationship between binary and hexadecimal (4 binary digits = 1 hex digit)
Quiz
- What prefix is used for hexadecimal literals in C++?
- Convert decimal 15 to binary, octal, and hexadecimal.
- What does the hexadecimal digit 'A' represent in decimal?
- Why might you use binary literals when working with bit flags?
- What is the decimal value of the octal number 0755?
Practice exercises
Try working with different numeral systems:
- Create variables using literals in all four numeral systems that represent the same value
- Write a program that displays the same number in decimal, binary, octal, and hexadecimal
- Create RGB color values using hexadecimal literals and display their decimal equivalents
- Practice converting between different number bases manually, then verify with code
Explore More Courses
Discover other available courses while this lesson is being prepared.
Browse CoursesLesson Discussion
Share your thoughts and questions