DevLab
Numbers

Number Bases Explained: Binary, Octal, Decimal, and Hex

Understand how number systems work — from binary and octal to decimal and hexadecimal — with practical examples for developers.

What Is a Number Base?

A number base (or radix) determines how many unique digits are used to represent numbers. Decimal (base 10) uses digits 0-9. Binary (base 2) uses only 0 and 1. Hexadecimal (base 16) uses 0-9 and A-F. The value of each digit depends on its position — just like in decimal, where "42" means 4×10 + 2×1.

Developers encounter multiple number bases constantly: binary for bitwise operations, hex for colours and memory addresses, octal for Unix file permissions, and decimal for everything else.

Binary (Base 2)

Binary is the language of hardware. Every CPU instruction, memory address, and network packet is ultimately a sequence of 0s and 1s. In JavaScript, you write binary literals with the 0b prefix: 0b1010 equals 10 in decimal.

Binary is essential for bitwise operations: AND (&), OR (|), XOR (^), and bit shifting (<<, >>). These operations are used in feature flags, permission systems, and low-level protocols.

Conversion: each binary digit represents a power of 2. 1010 = 1×8 + 0×4 + 1×2 + 0×1 = 10.

Octal (Base 8)

Octal uses digits 0-7. Its main use in modern development is Unix file permissions. The permission 755 means owner can read/write/execute (7 = 4+2+1), group can read/execute (5 = 4+1), and others can read/execute (5 = 4+1).

In JavaScript, octal literals use the 0o prefix: 0o755. The older 0755 syntax (without the "o") is forbidden in strict mode because it caused subtle bugs — 0777 looks like decimal but is octal.

Hexadecimal (Base 16)

Hex uses digits 0-9 and letters A-F (case-insensitive). Each hex digit represents exactly 4 binary digits (bits), which makes hex a compact way to write binary values. One byte (8 bits) is exactly two hex digits: FF = 11111111 = 255.

Common uses: CSS colours (#FF6B4A), memory addresses (0x7FFF5FBFF8A0), Unicode code points (U+1F600), cryptographic hashes, and UUIDs.

In JavaScript: 0xFF equals 255. Use Number.toString(16) to convert to hex and parseInt("FF", 16) to parse hex strings.

Quick Conversion Table

  • 0 = 0000 = 0x0
  • 5 = 0101 = 0x5
  • 10 = 1010 = 0xA
  • 15 = 1111 = 0xF
  • 255 = 11111111 = 0xFF
  • 256 = 100000000 = 0x100

Why Developers Need Multiple Bases

You do not "choose" a number base like you choose a framework — different contexts demand different bases. Hex is compact and maps cleanly to bytes. Binary makes bit-level operations visible. Octal is the standard for file permissions. Decimal is for human-facing values. The Number Base Converter tool lets you convert between all four instantly.

Frequently Asked Questions

Why do computers use binary instead of decimal?

Digital circuits use transistors that have two stable states: on (high voltage) and off (low voltage). Binary (base 2) maps directly to these two states, making hardware design simpler and more reliable. Decimal would require circuits that reliably distinguish between 10 different voltage levels, which introduces noise sensitivity and manufacturing complexity. Binary is not faster in theory — it just matches the physics of how transistors work.

When would a developer actually use octal (base 8)?

Octal appears primarily in Unix file permissions. The permission rwxr-xr-x translates to octal 755, where each octal digit maps to three binary bits representing read, write, and execute. You also see octal in C/C++ string escape sequences (\077) and some older programming conventions. Outside of these specific contexts, octal is rarely used in modern development.

What is the relationship between hexadecimal and binary?

Each hexadecimal digit represents exactly 4 binary bits, so conversion is a direct substitution. 0xF = 1111, 0xA = 1010, 0x3 = 0011. This makes hex a compact way to write binary values — the 32-bit integer 11111111000000001010101001100110 becomes FF00AA66, which is far easier to read and type. This is why hex is standard for memory addresses, color codes, and byte-level debugging.

Practice with these tools

More Learning Topics

RegexRegex Basics: A Complete Beginner's GuideRegexRegex Special Characters: Complete ReferenceRegexRegex Groups and Captures ExplainedRegexRegex Quantifiers: Complete GuideCSSCSS Selectors: The Complete GuideCSSCSS Specificity: Why Your Styles Aren't ApplyingJSONJSONPath Syntax: Query JSON Like XPathTimeUnix Timestamps ExplainedEncodingBase64 Encoding ExplainedEncodingJWT Structure and How It WorksEncodingJWT vs Session Tokens: Which Should You Use?EncodingJWT Refresh Tokens ExplainedCryptoHash Functions Explained: MD5, SHA-256, and When to Use EachEncodingURL Encoding Explained: What %20 Actually MeansJSONJSON Schema Explained: Validate Your JSON DataJSONJSON vs YAML: Which Should You Use?JSONJSON.stringify and JSON.parse: Edge Cases You Should KnowRegexRegex Lookahead and Lookbehind: Match Without ConsumingRegexRegex for Email Validation: The Right ApproachCSSThe CSS Box Model: margin, padding, border, and contentCSSFlexbox vs CSS Grid: When to Use EachCSSCSS Custom Properties (Variables) ExplainedTimeISO 8601 Explained: The Right Way to Format DatesTimeUnix Timestamps vs ISO 8601: Which to Use in Your API?EncodingUTF-8 Explained: How Computers Store TextTextCORS Explained: Why Your API Call is BlockedTextHTTP Status Codes: A Practical Developer GuideRegexNamed Capture Groups in Regex: Clean ExtractionsColorsColor Spaces Explained: RGB, HSL, HEX, and BeyondColorsColor Contrast for Developers: WCAG Rules and How to CheckNumbersBitwise Operations for Web DevelopersHTMLHTML Semantic Elements: A Complete ReferenceTextText Encoding for Developers: ASCII, UTF-8, and UnicodeRegexWhat is Regex? Complete Guide for DevelopersJSONJSON Format Explained: Structure, Syntax, and Common ErrorsEncodingHow JWT Works: Header, Payload, Signature DecodedHTMLHow to Convert Any Website to Markdown (for LLMs, RAG & Docs)TextPreparing Website Content for RAG: Clean Markdown Pipelines