Decimal, hexadecimal and binary numbers

Posted on

Table of contents

1 Numbers

Numbers are a way of expressing a quantity of things. While we could write these quantities in full words, like They have five apples, we can also use numerals, like They have 5 apples.

Wether you should use full words or numerals is a matter of preference. In math and computer science however, we have generally agreed on always using numerals.

While most numeric representations use the digits 0 to 9, there are some exceptions. But before we get to those exceptions, lets first look at the numeric representation we already know.

2 Decimal numbers, our normal numbers

When we write a numeral, we generally use one of the following symbols: 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9. These are called digits.

There are numbers that only contain one digit: the natural numbers below 10.

When we are increasing a number, we use the next digit in the list of digits for the new number.

0+1=1 1+1=2 2+1=3 ... 8+1=9
Increasing a number from 0 to 9

But now we have a problem, since there is no digit after 9!

Technically speaking, every number has an infinite amount of zeroes to the left. 0009 is exactly the same as 9.

Knowing this, we can increase the digit to the left by one, and restart the digit we were already working with with the lowest digit!

009+1=010 9+1=10 98+1=99 99+1=100
Increasing a number bigger than 10.

By following this pattern, we can keep counting forever with just 10 digits!

3 Hexadecimal numbers

Hexadecimal numbers follow the same rules as decimal numbers, they just have more digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E and F.

Yep! You read that right. We just use the symbols we normally use for letters as digits here.

We also prepend hexadecimal numbers with a $-sign, so we cannot mistake them for decimal numbers. $100, a hexadecimal number, is a lot bigger than 100, a decimal number.

So what happens if we have a hexadecimal number $9 and we increase it by 1? Or what happens when we increase $F by 1?

$8+1=$9 $9+1=$A $A+1=$B ... $F+1=$10
Increasing hexadecimal numbers

The same thing as with decimal numbers! When we go from digit F back to 0, we increase the value of the next digit.

Keep in mind that $10 is not equal to 10! $10 is equal to 16, since we also had to count over the extra, alphanumeric digits.

Some sources do not prefix hexadecimal numbers with a $-sign, but with 0x, 0h, 16r or h.

4 Binary numbers

We saw that decimal numbers and hexadecimal numbers follow the same rules. So do binary numbers, but again with a different amount of digits. Binary numbers only have access to the digits 0 and 1.

Binary numbers are prefixed with a %-sign. Keep in mind there is a big different between 100, $100 and %100!

Counting with binary numbers looks as follows:

%0+1=%1 %1+1=%10 %10+1=%11 %11+1=%100 %100+1=%101
Increasing binary numbers

Some sources do not prefix binary numbers with a %-sign, but with 0b or bin. In URI's you might see some number being prefixed by a %-sign as well, but that is a hexadecimal number, to make things confusing.

5 Converting between decimal, hexadecimal and binary numbers

Most OSses have a calculator that supports converting between numbers. Windows has calc.exe and Ubuntu has Calculator.

For both calculators you have to go to the settings and set the mode to programming-mode.

We can also do it by hand. Let's see how we accomplish that!

5.1 Converting decimal to hexadecimal

When converting from and to hexadecimal, it is important to know the powers of 2. We get these by repeatedly multiplying by 2.

137 -128 9 -8 1
Example to show that 128+8+1 equals 137
Spreading 137 over the powers of 2
128 64 32 16 8 4 2 1 Result
Binary 1 0 0 0 1 0 0 1 %10001001
Hexadecimal 8 9 $89

While this trick is handy for small decimal numbers, it is difficult to do this for numbers like 143798. There is a different way of getting the hexadecimal number that would normally be more complex, but is easier in the case of big numbers.

The steps to find it are as follows:

  1. Take a decimal number;
  2. Execute a long division on the decimal number until you get a remainder less than 16;
  3. If the quotient is bigger than 16 repeat these steps for the quotient;
  4. Take all the remainders from right to left to get the hexadecimal number.

This is how the steps would look for 143798:

16 8987 143798 -128 157 -144 139 -128 118 -112 6 16 561 8987 -80 98 -96 27 -16 11 16 35 561 -48 81 -80 1 16 2 35 -32 3
Long divisions to get the hexadecimal number from a decimal number

If we take all the remainders, we get our digits for the hexadecimal number. Don't forget to convert the numbers above 9 to hexadecimal digits! One of our remainders is 11, which is $B in hexadecimal. Which means that we got digit B out of it.

The digits are as follows: 6, B, 1, 3, 2.

If we swap the order around, we get our hexadecimal number: $231B6!

5.2 Converting hexadecimal to decimal

This one is a lot easier if we first look at decimal numbers. Lets take 864.

We can write that number in the following ways:

  • 800+60+4
  • 8×100+6×10+4×1
  • 8×102+6×101+4×100

What is great about this way of showing a number is that it applies to all number systems. We can do the same for the hexadecimal number $231B6:

  • $20000+$3000+$100+$B0+$6
  • $2×$10000+$3×$1000+$1×$100+$B×$10+$6×$1
  • $2×$104+$3×$103+$1×$102+$B×$101+$6×$100
  • 2×164+3×163+1×162+11×161+6×160
  • 2×65536+3×4096+1×256+11×16+6×1
  • 131072+12288+256+176+6
  • 143798

Which means that $231B6 equals 143798.

6 Sources