# Numeric Systems

An overview of numeric systems.

# Octal and Binary Numbers

Octal numbers are those whose base is 8. Octal numbers have a set of only 8 digits; they are 0, 1, 2, 3, 4, 5, 6 and 7. Binary numbers are those whose base is 2. They have only 2 digits, 0 and 1. We can also refer to each digit as being turned off or on.

```
Decimal | Binary | Octal
-------------------------
0 | 00000 | 0
1 | 00001 | 1
2 | 00010 | 2
3 | 00011 | 3
4 | 00100 | 4
5 | 00101 | 5
6 | 00110 | 6
7 | 00111 | 7
8 | 01000 | 10
9 | 01001 | 11
10 | 01010 | 12
11 | 01011 | 13
12 | 01100 | 14
```

The first digit in octal, matches the 3 digits of its binary equivalent from left to right, the next one the next 3 remaining binary digits and so on.

## Binary/Decimal conversion table

First of all, we will draw an example grid to have at hand in order to convert from binary to decimal, assuming that the LSB (Less Significant Bit) is at the right side and that we will work with byte-length binary numbers:

```
2^8 | 2^6 | 2^5 | 2^4 | 2^3 | 2^2 | 2^1 | 2^0 | Sum
-----------------------------------------------------
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 | 256
0 | 1 | 0 | 1 | 0 | 0 | 0 | 0 | 81
```

In this example, we convert the binary number 1010000 to decimal. To get decimal numbers from binary ones, we have to place the binary number within the lower row of this grid and sum all the decimal numbers above digits 1. Now that we understand how easy is to convert binary numbers to/from decimals, let’s get back to octals.

## Conversion from Decimal to Octal

Example: Get the octal number for the decimal 243

When translating decimals to binary numbers, we should start “turning on” the bit below the nearest decimal to the number we want to convert. In this case, it is 128. Then we see whether adding the next right decimal of the grid doesn’t exceed our original number (243).

So, we have 128 + 64 = 192. We keep repeating the process confidently while our sum keeps producing a result below our decimal number (243).

Now we have 128 + 64 + 32 + 16 = 240

If we add 8, our result would be 248, greater than 243, so we just leave this bit “turned off” (0) and try the next one, going always from left to right. This way, we end up turning on the bit below 2 and 1, so we finally have our desired binary number:

```
11110011
2^8 | 2^6 | 2^5 | 2^4 | 2^3 | 2^2 | 2^1 | 2^0 | Sum
-----------------------------------------------------
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 | 256
1 | 1 | 1 | 1 | 0 | 0 | 1 | 1 | 243
```

Now getting our octal number is the easiest part. We split our binary number into groups of 3, from right to left:

` 11110011 = [11] [110] [011] `

Now we just convert our splited parts back to decimal and write down each resulting digit below the corresponding binary part.

```
11110011 = [11] [110] [011]
3 6 3 = Octal value: 363
```

Although this is a very good practice from the binary point of view, there is a quicker method for conversion. The algorithm works as follows:

- Divide the decimal number by the base (8)
- Write down the remainder in reverse order
- Divide the result of step 1 by 8 again and go to step 2

Example:

```
243 / 8 = 30, r = 3
30 / 8 = 3, r = 6
3 / 8 = 0, r = 3
Octal number: 363
```

This example is a bit ambiguous as the number ends and begins with 3, so let’s convert 494 to octal:

```
494 / 8 = 61, r = 6
61 / 8 = 7, r = 5
7 / 8 = 0, r = 7
Octal number: 756
```

## Conversion from Octal to Decimal

Example: Get the decimal number for the octal 755

We take each digit and convert it to a binary number, from right to left just to avoid making mistakes.

```
[111] [101] [101]
7 5 5
```

Now we place the three binary parts in the same order into the grid and perform a basic binary/decimal conversion. We place the binary number making it match the right side. As we have 9 digits, we would append the next power for the 9th bit (2^9 = 256) to the left column of the grid.

So, 755 is 111101101 = Decimal number: 493

Of course, we can use en easier algorithm to avoid this long but demonstrative process:

- Multiply 8 by the first digit and add the next digit to the result
- Multiply 8 by the result of that addition and add the next digit
- go to step 2 until there are no more digits.

So that would be:

```
8 * 7 + 5 = 61
8 * 61 + 5 = 493
Decimal number: 493
```

Let’s just take a larger octal, 32565 for example:

```
8 * 3 + 2 = 26
8 * 26 + 5 = 213
8 * 213 + 6 = 1710
8 * 1710 + 5 = 13685
Decimal number: 13685
```

## About bit order

Depending on the computer architecture, the LSB (Less Significant Bit) may be on the left or right side. For these examples, we just assume that it is on the right side.

# Hexadecimal numbers

Hexadecimal numbers are in base 16. That means that each hexadecimal digit has 16 units. So how do we represent numbers, 10, 11, 12, 13, 14, and 15? We use the letters A, B, C, D, E and F respectively. We won’t cover decimal/binary conversion here at is it already explained on the *Octal and Binary Numbers* section. But we will also explain hexadecimal numbers around binary numbers, so here it is basic reference table.

```
Decimal | Binary | Hexa
-------------------------
0 | 00000 | 0
1 | 00001 | 1
2 | 00010 | 2
3 | 00011 | 3
4 | 00100 | 4
5 | 00101 | 5
6 | 00110 | 6
7 | 00111 | 7
8 | 01000 | 8
9 | 01001 | 9
10 | 01010 | A
11 | 01011 | B
12 | 01100 | C
13 | 01101 | D
14 | 01110 | E
15 | 01111 | F
16 | 10000 | 10
17 | 10001 | 11
Hexadecimal numbers are even easier to work with as each digit is
represented by 4 bits. (a nibble or half a byte).
```

## Conversion from Decimal to Hexadecimal

```
Example: Get the hex number for the decimal 253
We translate the number to binary, so we have:
253 = 11111101
Now we split it in groups of four bits:
[1111] [1101]
We get the decimal value for each part and write it down remembering that
10, 11, 12, 13, 14, and 15 are represented by digits A, B, C, D, E and F
respectively:
[1111] [1101]
F D
Hexadecimal number: FD
```

By using the same octal algorithm, you can also get hexadecimal numbers out of decimals without trouble:

- Divide the decimal number by the base (16)
- Write down the remainder in reverse order
- Divide the result of step 1 by 16 again and go to step 2

Example:

```
253 / 16 = 15, r = 13 (D)
15 / 16 = 0, r = 16 (F)
Hexadecimal number: FD
```

## Conversion from Hexadecimal to Decimal

```
Example: What is the decimal value of F36A?
By using the binary approach:
Get the binary value for each hexadecimal digit and convert the resulting
big binary number to decimal:
[1111] [0011] [0110] [1010] = 1111001101101010 = 62314
F 3 6 A
Decimal number: 62314
```

By using the multiplication algorithm:

- Multiply 16 by the first digit and add the next digit to the result
- Multiply 16 by the result of that addition and add the next digit
- go to step 2 until there are no more digits.

Example:

```
16 * 15(F) + 3 = 243
16 * 243 + 6 = 3894
16 * 3894 + 10 (A) = 62314
Decimal number: 62314
```

# Scientific Notation

The scientific notation was conceived to easily represent numbers that are either too large or too small.

For example:

- Approximate number of stars contained in the Andromeda Galaxy: 200,000,000,000 stars.
- The weight of an alpha particle, as emited in the radioactive decay of Plutonium-239: 0.000,000,000,000,000,000,000,000,006,645 kilograms.

A number in the scientific notation contains 3 elements:

```
1.23E-6
[ 1.23 ] [E] [-6]
a b c
```

- The multiplicand: should be lower than 10 and greater than 0
- The scientific notation symbol. It is “EXP” sometimes. The exponent is always 10. Sometimes, it is written just like 1.23 x 10^-6
- The index of the exponent, which may be positive (+) or negative (-)

## Conversion from plain decimals to the scientific notation

Example #1: Convert 200,000,000,000 to the scientific notation:

The multiplicand should be lower than 10, so we take the first number, which is 2.

`2`

We count the number of zeroes from the number we choosed (2). There are 11 of them.

Now we have the elements needed to conform our scientific expression:

`Result: 2E+11`

Example #2: Convert 0.005,432 to the scientific notation:

We take 5.432 as the multiplicand as it is < 10 and > 1.

`5.432`

There are 3 zeroes to the left. As the zeroes are to the left, we use a negative index:

`Result: 5.432E-3`

## Conversion from the scientific notation to plain decimals

Example #1: What is the value of 3.434E+5?

Write multiplicand.

`3.434`

Move the point as many times as indicated by the index to the right (it is positive).

`3434__.`

Fill the blank spaces with zeroes (you left 2 of them).

```
343400
Result: 343400
```

Example #1: What is the value of 9.3E-9?

Write multiplicand.

`9.3`

Move the point as many times as indicated by the index to the left (it is negative).

`.________93`

Fill the blank spaces with zeroes (you left 8 of them).

```
.0000000093
Result: 0.0000000093
```

Of course you can place a zero (0) each time you move the dot to the left or to the right, making the conversion in one shot that way.

# References

The U.S. Institute for Energy and Environmental Research