Math Tools Math Tools

Binary, Hexadecimal, and Why Programmers Love Base 16

Binary, Hexadecimal, and Why Programmers Love Base 16

By Math Tools ·

Binary, Hexadecimal, and Why Programmers Love Base 16

Here's a 32-bit number written in binary:

11011110101011011011111011101111

Now here it is in hexadecimal:

0xDEADBEEF

Same value (3,735,928,559 in decimal), but one is unreadable and the other is so memorable that programmers have used it for decades as a "magic number" to mark memory. That's the whole case for base 16: it's the most human-friendly way to write what computers actually store.


Hex Is Easier Than Decimal for Computer Data

Decimal feels natural to us, so you'd expect it to be the easiest way to read computer values. For numbers that come from bits, it's often the hardest.

Converting binary to decimal requires real arithmetic, and the decimal digits don't line up with bit patterns at all. Converting binary to hex requires no arithmetic: just split the bits into groups of four. Because 16 = 2⁴, each hex digit maps to exactly 4 bits. The structure of the data stays visible.


How Positional Number Systems Work

Every positional system works the same way: each position is worth a power of the base.

Decimal:  2,026 = 2×10³ + 0×10² + 2×10¹ + 6×10⁰
Binary:   1011₂ = 1×2³  + 0×2²  + 1×2¹  + 1×2⁰ = 11
Hex:      2F₁₆  = 2×16¹ + 15×16⁰ = 47

Base 16 needs 16 digit symbols. After 0–9, it uses letters:

Hex Decimal Binary
0 0 0000
5 5 0101
9 9 1001
A 10 1010
B 11 1011
C 12 1100
D 13 1101
E 14 1110
F 15 1111

See the full table with the table of bases.


The Four-Bit Trick

To convert binary to hex, group the bits in fours from the right, and translate each group:

1101 1110 1010 1101 1011 1110 1110 1111
  D    E    A    D    B    E    E    F

To go back, expand each hex digit into its 4 bits. No multiplication or division needed. Try it with the binary to hexadecimal converter or the binary to hex table.

Two hex digits make exactly one byte (8 bits), covering 00 to FF, or 0 to 255.


Converting Hex to Decimal

Multiply each digit by its power of 16:

0x1F4 = 1×256 + 15×16 + 4×1 = 256 + 240 + 4 = 500

Going the other way, divide by 16 repeatedly and read the remainders from bottom to top:

500 ÷ 16 = 31 remainder 4
 31 ÷ 16 =  1 remainder 15 (F)
  1 ÷ 16 =  0 remainder 1
→ 0x1F4

Check with the decimal to hexadecimal converter or hexadecimal to decimal converter.


Where Programmers Use Hex

  • Colors: web color #FF5733 is three bytes: red FF (255), green 57 (87), blue 33 (51)
  • Memory addresses: debuggers show addresses like 0x7ffe5a3c
  • Bit masks: 0x0F keeps the low 4 bits of a byte; 0xF0 keeps the high 4
  • MAC addresses: six bytes, like 00:1A:2B:3C:4D:5E
  • IPv6 addresses: 128 bits written as 32 hex digits in groups of four
  • Unicode: the snowman ☃ is U+2603
  • Hashes: a SHA-256 digest is 256 bits, written as 64 hex characters

What About Octal?

Base 8 groups bits in threes. It was popular on early computers with word sizes like 12, 18 and 36 bits, which divide evenly by 3. You still meet it in Unix file permissions: chmod 755 means read-write-execute (7 = 111) for the owner and read-execute (5 = 101) for others. Explore it with the binary to octal converter.


An Insider Reference: How Hex Won

Hex became dominant with IBM's System/360 family, announced in 1964. It standardized the 8-bit byte, and since 8 is divisible by 4 but not 3, two hex digits described a byte neatly while octal didn't. IBM's documentation and memory dumps used hex, and the rest of the industry followed.

The choice of letters A–F wasn't universal at first. In a 1968 letter to Communications of the ACM, Bruce Alan Martin of Brookhaven National Laboratory argued that reusing letters was confusing and proposed brand-new symbols for the digits 10 through 15, shaped to reflect their bit patterns. The idea never caught on. A–F won because every keyboard and printer already had them.


Two Concepts Worth Knowing

Positional Notation

In positional notation, a digit's value depends on its position: the same digit means more further left. All the number systems here, binary, octal, decimal and hex, are positional, differing only in their base.

Nibble

A nibble is 4 bits, half a byte, and exactly one hex digit. Thinking in nibbles is what makes hex-binary conversion instant.


Quick Answer: Why Do Programmers Use Hexadecimal?

Programmers use hexadecimal because each hex digit represents exactly four binary bits, so binary data can be written compactly and converted without arithmetic. Two hex digits make one byte (00 to FF). Hex is used for colors, memory addresses, bit masks, MAC and IPv6 addresses, and hash values.


Try Them Yourself

Pick your favorite web color, split its hex code into three bytes, and convert each to decimal. You'll know exactly how much red, green and blue your screen is mixing.