Why binary, octal and hexadecimal

A base is simply how many digits are available before you have to move to the next position. Base ten has ten, base two has only zero and one, base sixteen needs sixteen and borrows the letters A to F for the last six. A number's value does not change with the base: only how it is written does.

Binary exists because a circuit can tell two states apart reliably and ten states cannot. Hexadecimal exists because binary is unreadable to a human: each hexadecimal digit corresponds to exactly four bits, so converting between the two is a direct substitution group by group with no arithmetic. The same applies to octal, where one digit is three bits.

Two's complement is how computers represent negative numbers, and its elegance is that addition works with no special rule for signs. A negative number is obtained by inverting all the bits of its magnitude and adding one: in eight bits, −5 becomes 11111011. It follows that the range is not symmetric, because zero occupies a slot in the positive half: with n bits you go from −2ⁿ⁻¹ to 2ⁿ⁻¹−1.

Common mistakes

  • Treating the sign bit as a minus sign: in two's complement the most significant bit is worth −2ⁿ⁻¹, not “minus”. That is why 10000000 in eight bits is −128 and not −0.
  • Forgetting the positive range is one short: eight signed bits reach 127, not 128. It causes most of the overflows in homework.
  • Reading hexadecimal as decimal: 10 in hexadecimal is 16, not ten. Where the context is unclear, the prefixes 0x and 0b are worth writing.

Frequently asked questions

How do you convert binary to decimal?

Multiply each digit by the power of two matching its position, counting from zero at the right, and add the results. 10110110 is 128+32+16+4+2 = 182.

How do you convert decimal to binary?

Divide repeatedly by two writing down the remainders, then read the remainders from last to first. Alternatively subtract powers of two starting from the largest that fits.

How do you work out the two's complement of a negative number?

Write the magnitude in binary at the chosen width, invert every bit and add one. For −5 in eight bits: 00000101 becomes 11111010, and adding one gives 11111011.

Why is hexadecimal used so much in computing?

Because one hexadecimal digit is exactly four bits, so a byte is written with just two characters. Converting to binary needs no arithmetic at all, unlike converting to decimal.

How this calculation works

Value of a number in base b with digits cₙ…c₁c₀: V = Σ cᵢ · bⁱ, with i starting at zero on the right. Decimal to base b: repeated division by b, reading the remainders in reverse. Direct correspondence: one hexadecimal digit is 4 bits and one octal digit is 3 bits, so binary converts to either by grouping bits. Two's complement in n bits: for a non-negative v the pattern is v itself; for a negative v it is 2ⁿ + v, obtained by inverting the bits of |v| and adding 1. Signed range: −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1.