# Binary

## Decimal system

We use 10 different symbols (Digits) to represent numbers. They range from 0 to 9. That is called the decimal system.

## Binary system

Computers, however, use only 2 different symbols (Digits) to represent data (Numbers), which are 0 and 1.

## Counting in binary

When we count from zero and reach the last figure we can use to represent a number (9 in decimal), we use an additional digit to represent the number (10). It is the same in binary, except that it happens way more often (There are only two usable figures). The numbers 0 and 1 are the same in binary and decimal. However, when the number 2 is reached, the binary system already runs out of figures. We then need an additional digit to represent the number. In binary, the number 2 is represented as 10b. The b after the 10 is only there to specify that the number is in binary so we do not think it means ten.

A digit of a binary number is called a bit and a group of 8 bits is called an octet.

## Decimal / binary

Decimal Binary
0 0
1 1
2 10
3 11
4 100
5 101
6 110
7 111

The decimal number 11111 is made of (1 x 10000 + 1 x 1000 + 1 x 100 + 1 x 10 + 1) which is equivalent to (1 x 10 4+ 1 x 10 3+ 1 x 10 2+ 1 x 10 + 1). The binary number 11111b is made of (1 x 2 4+ 1 x 2 3+ 1 x 2 2+ 1 x 2 + 1), which is equivalent to 31 in decimal.

## Hexadecimal system

The hexadecimal system is frequently used in low-level software development. It uses 16 figures to represent numbers. The first ones are [0, 9] and the others after that are the letters [a, f]. The equivalent, in decimal, of a is 10 and the equivalent of f is 15. In hexadecimal, after f comes 10. 10 in hexadecimal equals 16 in decimal. To differentiate decimal from hexadecimal, hexadecimal numbers are often prefixed with 0x or suffixed with h (Ex. 0x56, 56h).

## Decimal / hexadecimal

Decimal Hexadecimal
1 1
2 2
10 a
11 b
12 c
15 f
16 10
17 11
27 1b

The decimal number 12345 is made of (1 x 10000 + 2 x 1000 + 3 x 100 + 4 x 10 + 5) which is equivalent to (1 x 10 4+ 2 x 10 3+ 3 x 10 2+ 4 x 10 + 5). The hexadecimal number 0x12345 is made of (1 x 16 4+ 2 x 16 3+ 3 x 16 2+ 4 x 16 + 5), which is equivalent to 74565 in decimal.

The reason hexadecimal is used is that it happens that any number that can be represented in binary using n octets (8n bits) can be represented in hexadecimal using n * 2 hexadecimal digits. So if we have a number that is represented in hexadecimal using 16 digits, we know we can represent it in binary using 8 octets (64 bits).

## Octal system

The octal system is less often used than hexadecimal, so it will only be introduced. It uses 8 digits [0, 7] to represent numbers. Similarly to hexadecimal, it can represent, using 4 digits, any number that can be represented in binary using 1 octet (8 bits).

## Conclusion

While being aware that computers work with the binary system is required, having a great understanding of the binary and hexadecimal systems is not essential as we almost never work directly with them. Being comfortable with them can, however, in some cases, be very useful.