Numbers and Arithmetic

Hexadecimal and Octal, the Shorthand Bases of Computing

Learn how base 16 and base 8 compress long binary strings, how to convert between them by hand, and where you meet each one.

Key takeaways

  • One hexadecimal digit equals four bits and one octal digit equals three bits, so conversion with binary is done group by group.
  • Hexadecimal uses the digits 0 to 9 and then the letters A to F for the values ten through fifteen.
  • To convert a decimal number to base 16 or base 8, divide repeatedly and read the remainders from last to first.
  • Hexadecimal shows up in colors, memory addresses and network identifiers, while octal survives mainly in Unix file permissions.

Hexadecimal (base 16) and octal (base 8) are compact ways of writing binary numbers. Because 16 and 8 are powers of 2, each hexadecimal digit stands for exactly four bits and each octal digit for exactly three. A bit is a single binary digit, 0 or 1.

That tidy match is the whole reason these bases exist in computing. People can read D6 far more reliably than 11010110, and they can still recover every bit from it without doing any real arithmetic.

What a base is

In a positional system, the value of a digit depends on where it sits. The base says how much each place is worth compared with the place to its right. In decimal the places are ones, tens, hundreds and thousands: powers of 10. The ideas are the same ones covered in how place value works.

In octal the places are powers of 8: 1, 8, 64, 512. In hexadecimal they are powers of 16: 1, 16, 256, 4096. A base needs as many distinct digit symbols as its size. Octal uses 0 through 7. Hexadecimal needs sixteen symbols, so after 9 it borrows letters: A is ten, B is eleven, C is twelve, D is thirteen, E is fourteen and F is fifteen. Lowercase letters mean the same thing.

The string 10 therefore means different amounts in different bases. It is ten in decimal, eight in octal, sixteen in hexadecimal and two in binary. This is why programmers mark the base. Many languages write 0x2A for hexadecimal and 0o52 for octal, and mathematicians often add a subscript, as in 2A₁₆ or 52₈. Both of those examples equal forty-two.

Why four bits and three bits

Four bits can form 2⁴ = 16 different patterns, from 0000 to 1111. Hexadecimal has exactly 16 digits. The patterns and digits pair off one to one with nothing left over. In the same way, three bits form 2³ = 8 patterns, matching the eight octal digits.

The reason you can convert group by group comes from the place values. Four binary places to the left multiply a value by 16, which is one hexadecimal place. So each block of four bits lines up with its own hexadecimal position and never spills into its neighbors. Decimal has no such luck, because 10 is not a power of 2. A change in one bit can alter several decimal digits at once.

Here are the sixteen patterns side by side. If you work with computers often, they become as familiar as a times table.

Decimal Binary Octal Hex
0 0000 0 0
5 0101 5 5
7 0111 7 7
8 1000 10 8
10 1010 12 A
12 1100 14 C
15 1111 17 F

Converting to and from binary

If you are comfortable with binary numbers and their conversions, the shortcut takes three steps.

  1. Split the binary number into groups, starting from the right: groups of four for hexadecimal, three for octal.
  2. Pad the leftmost group with zeros if it comes up short.
  3. Replace each group with its single digit.

Take the eight bits 11010110. For hexadecimal, split it as 1101 0110. The first group is 8 + 4 + 1 = 13, which is D, and the second is 4 + 2 = 6. The result is D6.

For octal, split the same bits as 11 010 110 and pad to 011 010 110. The groups are 3, 2 and 6, so the octal form is 326.

You can confirm that all three agree. In binary the number is 128 + 64 + 16 + 4 + 2 = 214. In hexadecimal, 13 × 16 + 6 = 214. In octal, 3 × 64 + 2 × 8 + 6 = 214.

Going the other way is just as direct. The hexadecimal number 3F becomes 0011 1111, so it equals 111111 in binary once you drop the leading zeros. To get from octal to hexadecimal, the easiest route is through binary: write out the bits and regroup them.

Binary 11010110 into hexadecimal and octal
1 Start with the bits 11010110, which is 214 in decimal 2 Group by four 1101 and 0110 give D6 in hexadecimal 3 Or group by three 011, 010 and 110 give 326 in octal 4 Check by place value 13 × 16 + 6 and 3 × 64 + 2 × 8 + 6 both give 214 Start with the bits 11010110, which is 214 in decimal 1 Group by four 1101 and 0110 give D6 in hexadecimal 2 Or group by three 011, 010 and 110 give 326 in octal 3 Check by place value 13 × 16 + 6 and 3 × 64 + 2 × 8 + 6 both give 214 4

Converting from decimal

Decimal numbers do not split into neat groups, so you use repeated division. Divide by the new base, record the remainder, and divide the quotient again until it reaches zero. The remainders, read from last to first, are your digits.

Here is 48879 converted to hexadecimal.

48879 ÷ 16 = 3054 remainder 15   (F)
 3054 ÷ 16 =  190 remainder 14   (E)
  190 ÷ 16 =   11 remainder 14   (E)
   11 ÷ 16 =    0 remainder 11   (B)
read upward: BEEF

To check, expand by place value: 11 × 4096 + 14 × 256 + 14 × 16 + 15 = 45056 + 3584 + 224 + 15 = 48879. Programmers enjoy that hexadecimal numbers sometimes spell words, and a few such values are used as recognizable markers in memory.

The method works because the first remainder is whatever is left after taking out all complete groups of 16, which is exactly the ones digit. The next division repeats the argument one place higher.

Adding in hexadecimal

Column addition works in any base. The only change is that you carry when a column reaches the base, not when it reaches ten. Add 3A and 4F:

ones column:      A + F = 10 + 15 = 25 = 16 + 9    write 9, carry 1
sixteens column:  3 + 4 + 1 = 8                    write 8
sum: 89

In decimal this is 58 + 79 = 137, and 89 in hexadecimal is 8 × 16 + 9 = 137.

Fractions follow the same pattern to the right of the point. The first place after a hexadecimal point is worth 1/16 and the next 1/256. So 0.8 in hexadecimal is 8/16, or one half. A fraction terminates in base 2, 8 or 16 only if its denominator, in lowest terms, is a power of 2. One tenth does not qualify, which has consequences for how computers store decimals.

Where you meet hexadecimal

A byte is a group of eight bits, so it is always exactly two hexadecimal digits, from 00 to FF (0 to 255). That fit makes hexadecimal the usual notation for raw data.

  • Colors on the web. A code such as #FF8800 holds three bytes for red, green and blue. FF is 255, 88 is 136 and 00 is 0, which gives an orange.
  • Memory addresses. Debuggers and error messages print locations in hexadecimal.
  • Network identifiers. A hardware (MAC) address is commonly written as six bytes, or twelve hexadecimal digits. An IPv6 address is 128 bits, written as up to 32 hexadecimal digits in eight groups.
  • Character codes. Unicode code points are written like U+0041, the capital letter A.
  • Digests. The outputs of hash functions are usually displayed as long hexadecimal strings.

Remember that hexadecimal describes a bit pattern, and the meaning comes from context. The byte FF is 255 if read as an unsigned number. In the common two's complement convention for signed 8-bit integers, the same pattern means −1.

Picking out bits with masks

Because digit boundaries match bit boundaries, hexadecimal makes it easy to see which bits an operation touches. Programmers often use a mask, a pattern chosen to keep, set or flip particular bits.

Take the byte D6 again, which is 1101 0110.

  • D6 AND 0F gives 06. The operation AND keeps a bit only where both inputs have a 1, so the mask 0F keeps the lower four bits.
  • D6 AND F0 gives D0, keeping the upper four bits instead.
  • D6 OR 01 gives D7. The operation OR sets a bit if either input has it, so the lowest bit is switched on.
  • D6 XOR FF gives 29. Exclusive OR with all ones flips every bit.

You can read each result one digit at a time, since no operation here carries from one bit to the next. Shifting works neatly too. Moving a number one hexadecimal place to the left shifts it by four bits and multiplies it by 16, so 2A becomes 2A0. The logic behind these operations is covered in Boolean algebra and digital circuits.

Where octal still appears

Octal was popular when many computers organized data in words of 12, 18 or 36 bits, lengths that divide evenly into groups of three. It has the advantage of needing no letters. Once the 8-bit byte became standard, hexadecimal fit better, since eight splits into two fours but not into threes.

The place you are most likely to see octal today is file permissions on Unix-like systems. Each file has three sets of permissions (owner, group and everyone else), and each set has three yes-or-no flags: read, write and execute. Three flags are three bits, so each set is one octal digit, with read worth 4, write worth 2 and execute worth 1.

A mode of 755 means the owner has 4 + 2 + 1 = 7 (everything), while the group and others each have 4 + 1 = 5 (read and execute). A mode of 644 gives the owner read and write, and everyone else read only.

One trap deserves care. In C and some related languages, an integer written with a leading zero, such as 010, is read as octal and means eight. Padding numbers with zeros to line them up can silently change their values.

Octal and hexadecimal at a glance
Octal Base 8, digits 0 to 7 One digit covers 3 bits Used for Unix file permissions Suited older word sizes such as 12 or 36 bits Hexadecimal Base 16, digits 0 to 9 and A to F One digit covers 4 bits Used for colors, addresses and raw data Two digits make exactly one byte VS Octal Base 8, digits 0 to 7 One digit covers 3 bits Used for Unix file permissions Suited older word sizes such as 12 or 36 bits Hexadecimal Base 16, digits 0 to 9 and A to F One digit covers 4 bits Used for colors, addresses and raw data Two digits make exactly one byte

Common mistakes

Grouping from the wrong end. For whole numbers, always group from the right. Grouping 110101 from the left gives 1101 01, which is wrong. The correct split is 0011 0101, or 35 in hexadecimal.

Reading the digits as decimal. The hexadecimal number 25 is not twenty-five. It is 2 × 16 + 5 = 37.

Treating the base as a property of the number. The quantity forty-two is the same however you write it. Only the numeral changes. Other bases make the same point in different settings, such as counting in twelves and sixties.

Converting digit by digit between decimal and hexadecimal. The group shortcut works only between bases that are powers of a common number. Decimal to hexadecimal needs real arithmetic.

A good habit is to estimate before converting. A four-digit hexadecimal number lies below 16⁴ = 65536, and a number starting with F in the top place is close to that ceiling. If your answer is far outside the expected range, recheck the place values.

Written for 4Mathematics with AI writing tools and not reviewed line by line by a person. Check anything you rely on against a textbook or primary source. Report an error.

Keep reading

All 38 articles in Numbers and Arithmetic →

Latest news on this topic