Binary, Hex, and Octal: Number Bases Explained
Photo by Unsplash on Unsplash
Table of Contents
What does a number base even mean?
> Quick answer: A number base is how many unique digits a system uses. Decimal (base 10) uses 0-9, binary (base 2) uses 0 and 1, octal (base 8) uses 0-7, and hexadecimal (base 16) uses 0-9 plus A-F. Computers run on binary because a wire is either on or off, and programmers use hex as a shorter, human-friendlier way to write those long binary strings. To flip a value between all four without doing the math by hand, a number base converter handles it in one step.
In everyday decimal, the number 253 means 2 hundreds, 5 tens, 3 ones — each place is a power of 10. Every base works the same way; only the 'per place' multiplier changes. Binary places are powers of 2, hex places are powers of 16. Once that clicks, converting is just arithmetic, and the whole thing stops feeling like a party trick.
Why do computers use binary?
Everything else your computer does is built on top of that. A single binary digit is a bit. Eight bits make a byte, which can represent 256 values (2 to the 8th), and that's the fundamental chunk memory is measured in. When you read that a file is a certain number of kilobytes, you're counting bytes, which are counting bits, which are counting on/off states.
The catch is that binary is miserable for humans to read and write. The number 200 in binary is `11001000` — eight characters for a small value, and easy to miscount. That readability problem is exactly why hex exists, and why you rarely see raw binary in code even though it's what's running underneath. Encodings like UTF-8 and ASCII are just agreed-upon ways of mapping those bytes to characters.
Why is hexadecimal so popular with programmers?
That clean mapping makes hex a compact shorthand for binary. That eight-bit byte `11001000`? Split it into two nibbles of four bits, `1100` and `1000`, and each becomes a single hex digit: C and 8. So `11001000` in binary is just `C8` in hex. Two readable characters instead of eight error-prone ones, and you can mentally translate back to binary any time because the grouping never shifts.
That's why hex shows up all over the place: memory addresses, color codes, byte values, and error codes are almost always written in hex. CSS colors are the everyday example — `#FF5733` is three bytes, one each for red, green and blue, written as six hex digits. I dug into that in the CSS color formats guide. Hex is the format that lets you eyeball a byte without drowning in ones and zeros.
How do you tell the bases apart in code?
The common prefixes are `0b` for binary (`0b1010`), `0o` for octal (`0o17`), and `0x` for hexadecimal (`0xFF`). Decimal gets no prefix; it's the default. So `0xFF` is unmistakably 255, `0b1010` is unmistakably 10, and a plain `255` is decimal. Most languages — JavaScript, Python, C and their relatives — share these prefixes, so the knowledge carries over.
Octal is the odd one out these days. It's base 8, using digits 0-7, and it was more common in older systems. You'll still meet it in one very live place: Unix file permissions, where `chmod 755` is octal. Each digit encodes three permission bits, which is another case of a base being chosen because it maps cleanly onto binary. Outside that, octal is rare, but it's worth recognizing so a `0o` prefix doesn't confuse you.
How do you convert between bases?
Going the other way, from decimal, you divide repeatedly by the target base and collect the remainders bottom-up. It works, but it's fiddly and easy to slip on, especially with longer numbers or hex's letter-digits. I can do small ones in my head and I still reach for help on anything past a byte.
That's the honest case for not doing it by hand in day-to-day work: the math is simple but unforgiving, and a single arithmetic slip gives you a silently wrong value. Pasting the number into a number base converter shows you binary, octal, decimal and hex side by side instantly, which is what you actually want when you're debugging a color code or a bitmask. Understand the mechanism once, then let the tool save you from the arithmetic.
Where do number bases show up in everyday code?
Bitmasks and flags are a big one. When a library packs several on/off options into a single number, it's using binary under the hood, and you often set them with hex or binary literals like `0b0110`. Bitwise operations — the `&`, `|`, and shift operators — only really make sense once you picture the binary. I ignored bitwise operators for years until a permissions system forced me to think in bits, and suddenly hex stopped being decoration.
Unicode code points are written in hex too. A character like the em dash is `U+2014`, and that `2014` is hexadecimal — the same reason character encodings name their thousands of characters in hex to stay compact. File formats, network protocols, and memory dumps all show bytes in hex for the same readability reason.
The practical upshot is that base fluency isn't academic — it's what lets you read a color, debug a bitmask, or make sense of a hex dump without freezing. You don't need to convert by hand; MDN's guide to numbers and strings in JavaScript covers the literal syntax, and a converter handles the arithmetic. What you need is the recognition — seeing `0x1F` and knowing that's a small number written for machines, not a typo.
Frequently Asked Questions
Why do programmers use hexadecimal instead of binary?
Because 16 is 2 to the 4th, exactly four binary digits map to one hex digit, so hex is a compact, readable shorthand for binary. A byte like `11001000` becomes just `C8`, which is far easier to read and less error-prone.
What do the 0x, 0b, and 0o prefixes mean?
They tell the language which base a number is written in: `0x` is hexadecimal (`0xFF` = 255), `0b` is binary (`0b1010` = 10), and `0o` is octal. Decimal needs no prefix. You can see all four side by side in the [number base converter](/tools/number-base-converter).
What is a byte in binary?
A byte is 8 bits, and each bit is one binary digit (0 or 1). Eight bits can represent 256 different values (2 to the 8th), which is why a byte is the fundamental unit memory is measured in.
Where is octal still used?
The most common place is Unix file permissions, like `chmod 755`, where each octal digit encodes three permission bits. Outside that, octal is fairly rare in modern code, but the `0o` prefix marks it when it appears.
How do I convert hex to decimal quickly?
Multiply each digit by its place value (powers of 16) and add them, remembering A-F stand for 10-15. For anything longer than a byte it's easy to slip, so a [number base converter](/tools/number-base-converter) is the reliable route. Understanding the place-value method once means the converter's output always makes sense to you rather than being a black box.
Try ToolsFuel
23+ free online tools for developers, designers, and everyone. No signup required.
Browse All Tools