Number Bases Explained — Binary, Octal, Decimal & Hexadecimal for Developers
Understand number bases (binary, octal, decimal, hexadecimal) and how to convert between them. Includes examples and our free number base converter tool.
What Are Number Bases?
A number base (or radix) is the number of unique digits used to represent numbers in a positional numeral system.
The most common bases in computing:
| Base | Name | Digits | Use Case |
|---|---|---|---|
| 2 | Binary | 0-1 | Digital circuits, bitwise operations |
| 8 | Octal | 0-7 | Historical (Unix file permissions) |
| 10 | Decimal | 0-9 | Human counting, everyday math |
| 16 | Hexadecimal | 0-9, A-F | Memory addresses, colors, assembly |
Why Different Bases Matter
Binary (Base 2)
Computers use binary because digital circuits have two states: on (1) and off (0).
Applications:
- Bitwise operations (&, |, ^, ~, <<, >>)
- Flag systems (each bit represents a boolean flag)
- Network subnet masks
- Binary file formats
Octal (Base 8)
Historically used in Unix systems because 8 bits fit into 3 octal digits.
Applications:
- Unix file permissions (chmod 755)
- Some legacy systems
Hexadecimal (Base 16)
Compact representation of binary. One hex digit = 4 binary digits (a "nibble").
Applications:
- Memory addresses
- Color codes (#FF5733)
- MAC addresses
- Assembly language
- UUIDs
Converting Between Bases
Method 1: Using FreeToolJet's Number Base Converter
Our Number Base Converter tool makes conversions instant:
#### Step-by-Step Guide
- Open the Number Base Converter tool
- Enter a number in any base (2, 8, 10, or 16)
- Select the input base
- View the conversions to all other bases simultaneously
- Copy the result you need
#### Features
- Real-time conversion: See results as you type
- Multiple bases: Convert to binary, octal, decimal, and hexadecimal
- Bit/byte display: See binary representation grouped by bytes
- Error handling: Invalid digits are flagged immediately
- Client-side only: Your numbers never leave your browser
Method 2: Manual Conversion
#### Decimal to Binary
Division method: Repeatedly divide by 2, collect remainders.
Example: Convert 42 to binary
42 ÷ 2 = 21 remainder 0 ↑
21 ÷ 2 = 10 remainder 1 |
10 ÷ 2 = 5 remainder 0 |
5 ÷ 2 = 2 remainder 1 |
2 ÷ 2 = 1 remainder 0 |
1 ÷ 2 = 0 remainder 1 |
|
Read from bottom to top: 101010
So 42₁₀ = 101010₂
#### Binary to Decimal
Position method: Multiply each bit by 2^position (right to left, starting at 0).
Example: Convert 101010₂ to decimal
1 0 1 0 1 0
↑ ↑ ↑ ↑ ↑ ↑
2⁵ 2⁴ 2³ 2² 2¹ 2⁰
32 + 8 + 2 = 42
`
So 101010₂ = 42₁₀
#### Decimal to Hexadecimal
Division method: Repeatedly divide by 16, collect remainders.
Example: Convert 255 to hexadecimal
255 ÷ 16 = 15 remainder 15 (F) ↑
15 ÷ 16 = 0 remainder 15 (F) |
|
Read from bottom to top: FF
So 255₁₀ = FF₁₆
#### Hexadecimal to Decimal
Position method: Multiply each digit by 16^position.
Example: Convert FF₁₆ to decimal
F (15) F (15)
↑ ↑
16¹ 16⁰
15×16 15×1
240 + 15 = 255
So FF₁₆ = 255₁₀
#### Binary to Hexadecimal (and vice versa)
Grouping method: Group binary digits into sets of 4 (nibbles).
Example: Convert 10101100₂ to hexadecimal
1010 1100
A C
So 10101100₂ = AC₁₆
Reverse: Convert AC₁₆ to binary
A (10) = 1010
C (12) = 1100
So AC₁₆ = 10101100₂
Number Base in Programming Languages
Python
# Decimal to other bases
decimal = 42
binary = bin(decimal) # '0b101010'
octal = oct(decimal) # '0o52'
# Other bases to decimal binary = int('101010', 2) # 42 octal = int('52', 8) # 42 hexadecimal = int('2a', 16) # 42
# Arbitrary base conversion (base 2-36)
decimal = int('101010', 2)
base3 = np.base_repr(decimal, 3) # '1120' (requires numpy)
`
JavaScript/Node.js
// Decimal to other bases
const decimal = 42;
const binary = decimal.toString(2); // '101010'
const octal = decimal.toString(8); // '52'
// Other bases to decimal const fromBinary = parseInt('101010', 2); // 42 const fromOctal = parseInt('52', 8); // 42 const fromHexadecimal = parseInt('2a', 16); // 42
// Note: JavaScript uses 32-bit signed integers for bitwise operations
// Maximum safe integer for bitwise: 2^31 - 1 = 2147483647
`
Java
// Decimal to other bases
int decimal = 42;
String binary = Integer.toBinaryString(decimal); // "101010"
String octal = Integer.toOctalString(decimal); // "52"
// Other bases to decimal
int fromBinary = Integer.parseInt("101010", 2); // 42
int fromOctal = Integer.parseInt("52", 8); // 42
int fromHexadecimal = Integer.parseInt("2a", 16); // 42
`
C/C++
#include <stdio.h>
int main() {
int decimal = 42;
// Decimal to other bases (output)
printf("Binary: %b
", decimal); // Not standard C, use custom function
printf("Octal: %o
", decimal); // "52"
printf("Hexadecimal: %x
", decimal); // "2a"
// Other bases to decimal (input)
int fromBinary = (int) strtol("101010", NULL, 2); // 42
int fromOctal = (int) strtol("52", NULL, 8); // 42
int fromHexadecimal = (int) strtol("2a", NULL, 16); // 42
return 0;
}
`
Go
import ( "fmt" "strconv" )
func main() {
decimal := 42
// Decimal to other bases
binary := strconv.FormatInt(int64(decimal), 2) // "101010"
octal := strconv.FormatInt(int64(decimal), 8) // "52"
hexadecimal := strconv.FormatInt(int64(decimal), 16) // "2a"
// Other bases to decimal
fromBinary, _ := strconv.ParseInt("101010", 2, 64) // 42
fromOctal, _ := strconv.ParseInt("52", 8, 64) // 42
fromHexadecimal, _ := strconv.ParseInt("2a", 16, 64) // 42
fmt.Println(binary, octal, hexadecimal)
fmt.Println(fromBinary, fromOctal, fromHexadecimal)
}
`
Practical Applications
1. Bitwise Operations
Understanding binary is essential for bitwise operations:
const a = 5; // 0101 in binary
console.log(a & b); // AND: 0001 = 1
console.log(a | b); // OR: 0111 = 7
console.log(a ^ b); // XOR: 0110 = 6
console.log(~a); // NOT: ...11111010 (32-bit: -6)
console.log(a << 1); // Left shift: 1010 = 10
console.log(a >> 1); // Right shift: 0010 = 2
`
2. Color Codes
Hexadecimal is used for colors in web design:
#FF5733
FF = Red (255)
57 = Green (87)
33 = Blue (51)
3. Unix File Permissions
Octal is used for file permissions in Unix/Linux:
chmod 755 file.txt
# 7 = rwx (owner)
# 5 = r-x (group)
# 5 = r-x (others)
Breakdown:
- 7₈ = 111₂ = rwx (read, write, execute)
- 5₈ = 101₂ = r-x (read, execute)
- 4₈ = 100₂ = r-- (read only)
4. IP Addresses and Subnets
Binary is used for subnet masks:
IP: 192.168.1.1
Mask: 255.255.255.0 (/24)
Binary: 11111111.11111111.11111111.00000000
5. Memory Addresses
Hexadecimal is used for memory addresses:
int x = 42;
printf("Address of x: %p
", &x);
// Output: Address of x: 0x7ffd2c3b4a2c
6. UUIDs
UUIDs are typically displayed in hexadecimal:
550e8400-e29b-41d4-a716-446655440000
Common Number Base Prefixes
| Base | Prefix | Example | Language |
|---|---|---|---|
| Binary | 0b or 0B | 0b101010 | Python, JavaScript, Java, Go |
| Octal | 0o or 0O | 0o52 | Python, JavaScript, Go |
| Octal (old) | 0 | 052 | C, C++, Java (deprecated) |
| Hexadecimal | 0x or 0X | 0x2a | Most languages |
| Hexadecimal | # | #FF5733 | CSS colors |
Bitwise Flags Example
Using binary for boolean flags:
const FLAG_READ = 1; // 0001
const FLAG_WRITE = 2; // 0010
let permissions = 0;
// Add permissions permissions |= FLAG_READ; // Now: 0001 permissions |= FLAG_WRITE; // Now: 0011 (read + write)
// Check permissions const canRead = (permissions & FLAG_READ) !== 0; // true const canExecute = (permissions & FLAG_EXECUTE) !== 0; // false
// Remove permissions
permissions &= ~FLAG_WRITE; // Now: 0001 (only read)
`
Two's Complement (Negative Numbers in Binary)
Computers represent negative numbers using two's complement:
8-bit signed integer:
Example: -42 1. Positive 42 in binary: 00101010 2. Invert bits (one's complement): 11010101 3. Add 1 (two's complement): 11010110
So -42 = 11010110 (in 8-bit two's complement)
`
Large Number Bases (Base 64)
Base64 uses 64 characters (A-Z, a-z, 0-9, +, /) to encode binary data as text.
Used for: - Email attachments (MIME) - Data URLs - Encoding binary data in JSON/XML
// Encode to Base64
// Decode from Base64
const decoded = atob('SGVsbG8='); // "Hello"
`
Tips for Working with Number Bases
- Memorize powers of 2: 2¹⁰ = 1024 (1 KB), 2²⁰ = 1,048,576 (1 MB)
- Use hexadecimal for debugging memory: More compact than binary
- Understand two's complement: Essential for low-level programming
- Be careful with signed/unsigned: Overflow behavior differs
- Use built-in functions: Don't write your own base conversion (prone to bugs)
- Test edge cases: Test with 0, 1, maximum values, and negative numbers
Common Mistakes
| Mistake | Problem | Fix |
|---|---|---|
| Confusing decimal and binary | 10 = ten (decimal) vs two (binary) | Always specify base |
| Off-by-one in bit positions | Forgetting positions start at 0 | Count from 0, not 1 |
| Signed integer overflow | Adding 1 to 2147483647 | Use larger integer types |
| Leading zeros in JavaScript | 0888 is interpreted as decimal | Always use 0o prefix for octal |
Hex without 0x prefix | FF is not a valid number | Use 0xFF |
Related Tools
- Number Base Converter — Convert between binary, octal, decimal, hex
- Color Converter — Convert hex color codes to RGB
- Hash Generator — Generate hashes (often displayed in hex)
- JSON Formatter — Format JSON data (numbers in decimal)