Guides Number systems

Number Systems for Programmers

Binary, octal, decimal, and hexadecimal are not separate puzzles; they are the same idea with different group sizes. Once you see the pattern, conversions become mechanical, and documentation stops feeling like a wall of cryptic literals.

What is a number system?

A positional number system writes values as a sequence of digits, where each position has a weight. In base b, the rightmost digit is the ones place, the next is b, then b2, and so on. Decimal is base-10 because each position is ten times the previous. Changing the base changes how many distinct digit symbols you allow before you roll over to the next position.

This matters for programming because computers ultimately store integers and data as bits. Humans, meanwhile, prefer compact notation. That tension is why you will see the same underlying pattern expressed in binary for hardware truth, hex for addresses and colors, and decimal for business logic and user-visible numbers.

Binary (base-2)

Binary uses two symbols, 0 and 1, aligned to powers of two. It maps directly to transistors, voltage levels, and memory cells, which is why low-level interfaces, file formats, and network protocols often describe structure in bits and bytes.

When someone says a CPU register is 32 or 64 bits wide, they are describing how many binary digits fit in a native integer container. Understanding binary helps you reason about alignment, padding, endianness, and why certain sizes (8, 16, 32, 64) keep appearing in APIs.

Octal, decimal, and hexadecimal

Decimal (base-10) is the default human notation. Most language literals without a prefix are decimal. It is excellent for economics and everyday quantities, but it does not align neatly with bit boundaries.

Octal (base-8) groups bits in threes. It shows up memorably in Unix file permissions, where triplets represent read, write, and execute for user, group, and others. You may also encounter octal in older systems or embedded tooling, even if hex dominates newer platforms.

Hexadecimal (base-16) groups bits in fours. Sixteen symbols (0-9 and A-F) cover all four-bit patterns. That one-to-one mapping makes hex the standard for memory dumps, color channels (#RRGGBB), and bitmask constants in C-like languages.

Converting between bases

To interpret a number written in base b as decimal, expand by place value. For example, binary 1011 is 1×8 + 0×4 + 1×2 + 1×1 = 11 in decimal.

To convert a positive decimal integer to another base, repeatedly divide by the target base and read the remainders from last to first. For fractions, multiply the fractional part by the base and collect integer parts, watching for repeating expansions.

Hex and binary interconvert especially fast: replace each hex digit with four bits, or group bits from the radix point outward. Octal uses three-bit groups. These shortcuts explain why engineers reach for hex or octal when they still think in bits.

Prefix conventions

Many languages disambiguate literals with prefixes. Common forms include 0b for binary, 0o for octal, and 0x for hex in C-like syntax. Python also accepts these forms; some older contexts used a leading zero for octal, which is easy to misread, so prefer explicit prefixes when available.

When reading docs, always confirm whether a value is decimal or hex. A bug caused by assuming the wrong base often looks like an off-by-one error but is actually a radix mistake.

Bitwise operations overview

AND keeps bits set only where both operands have 1. OR sets a bit if either operand has 1. XOR sets a bit if exactly one operand has 1. NOT flips bits within a fixed width. Shifts move bit patterns left or right, which is equivalent to multiplying or dividing by powers of two for unsigned integers until you hit overflow or sign rules.

These operations underpin flags, permissions, hashing, compression ideas, and performance tricks. Seeing operands in hex often makes masks easier to scan, because each digit is a neat nibble-sized chunk of bits.

Frequently asked questions

Why do programmers use hex instead of binary?

Hex is shorter than binary but still lines up with four-bit groups, so it is easier to read and type for addresses, colors, and constants without losing the bit-level picture.

What is octal used for?

Classic examples include Unix permission triplets and some legacy tooling. It represents three-bit groupings, which can be handy when data naturally packs in threes.

How do I convert between bases?

Use place-value expansion to decimal, and repeated division (or bit grouping for hex and octal) from decimal. Verify tricky values with a trusted converter or interpreter literal.

Related guides

Related tools