# Computer Math Basics: Binary, Decimal, Hexadecimal, Octal

How we express a number depends on whether we are a computer or a human. If we are human, we are likely to express numbers using our familiar 10-base decimal system. If we are a computer, we are likely, at our core, to express numbers as 2-base or binary.

So what is up with all the many ways of expressing numbers, and why do they exists? This article will go into some detail and hopefully by the end you’ll be counting octal on your fingers. Which works fine by the way, as long as you use only 8 fingers, after all… octal is 8-base.

In this tutorial you will learn:

• How to do simple counting in up non-decimal systems like binary, hexadecimal and octal.
• What the terms 2-base, 10-base etc. stand for and how to understand them more easily.
• The connection between these various methods of expressing numbers

Computer Math Basics: Binary, Decimal, Hexadecimal, Octal

## Software requirements and conventions used

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Linux Distribution-independent
Software Bash command line, Linux based system
Other Any utility which is not included in the Bash shell by default can be installed using `sudo apt-get install utility-name` (or `yum install` for RedHat based systems)
Conventions # – requires linux-commands to be executed with root privileges either directly as a root user or by use of `sudo` command
\$ – requires linux-commands to be executed as a regular non-privileged user

## Decimal

We are all super familiar with the decimal system: 1 to 10 or better 0 to 9, the very system we were thought from the earliest day of school and even before by our parents. But this numerical system is not all there is. It is just one of them. We call this particular system 10-base as it has a basis of 10 characters namely 0 to 9.

In Decimal, we can count easily by simply using what we were thought: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.

We do not need to exert effort for this, and it comes naturally. However, if you really think about it, there is no real logical connection between the word number “zero” and “one” and “one” and “two” and so on. Offcourse, in time we understand that 0+1=1 and 1+1=2, but there is no direct real and substantial other connection between one and two, 1 and 2. It is just a form of expression.

To exemplify this, consider the above assertions in comparison with a fictive 5-base system. It is a lot harder for our minds, as they have not been trained in the same, to count in a 5-base system. Let’s make it even one step harder and state that our 5 numbers are expressed as `(`, `)`, `+`, `=` and `.` respectively. Let’s count to 11 shall we?

```0: (
1: )
2: +
3: =
4: .
5: )(
6: ))
7: )+
8: )=
9: ).
10: +(
11: +)
```

On the left we have 10-base decimal numbers, on the right we have our self-generated 5-base numerical system counting up in the same way (and both the left and the right have equal numerical values, i.e. `10` in decimal/10-base is `+(` in our 5-base numerical system!).

I can count very easily this way as I am very used to how x-base systems work. If you look a little closer at the count, you will quickly discover how it works and see how it quite compares with our decimal based counting system. The clue is this; once you run out of characters, you simply prefix the first character with the first character, making two characters. Still, how would you write 100? Do you have to work all the way down the list? Likely as our minds are not used to enumerating things using these symbols.

Our minds understand decimal, and struggle with most other x-base based numerical systems where x is not 10. Perhaps an example? Please calculate `))(((A==-()B..(+` where we have used `A` to indicate multiplication, and `B` is simple plus. But there is nothing similar about it, right? Still, if we converted this to decimals and our familiar `+` and `x` symbols, we would likely not find this equations to hard.

Now that we are armed with an understanding of what x-base really is, the rest is much easier. And I promise: no more strange symbols to express numbers, well that is until we get to hexadecimal 😉

## Binary

Until quantum computers hit our local computer stores, our computers are quite limited. The only thing, at it’s very core, which a computer understand is power or no power. Nothing else! A computer simply understand power or no power, but it does not “understand” what a character `a` is, or what a digit `9` is. All of these things, and much more (i.e. all computer code) at it’s very core is expressed as many power or no power.

A single such unit of storage and expression is called a bit. A bit is the most low-level, core, storage unit of a computer. A bit can store only a single 0 or a single 1. In actually, it can not even store a zero or a one, it can only store power (our `1`), or no power (our `0`). You can start to see how 2-base, or binary, works: it only has two expressions: 0 and 1, no power or power.

If you picture this in terms of physical computer hardware, you can picture an older type hard disk drive as a plate full of many little places which either have power (are magnetized) or have no power (are not magnetized). If you picture it as data flowing over a cable, you can picture it as power or no power.

So let’s do our same counting to 11 but this time using our only two possible methods of expression, the numbers in our binary numerical system: 0 and 1.

```0: 0
1: 1
2: 10
3: 11
4: 100
5: 101
6: 110
7: 111
8: 1000
9: 1001
10: 1010
11: 1011
```

On the left we have 10-base decimal, and on the right we have 2-base binary.

Once you see it, it is easy to count: Simply start with 0 and 1, and note how 0 always has a special meaning: when you come to `2` in decimal, it is not `01` (i.e. the first character used as a new leftmost character), but rather `10` as 0 has the actual value of zero. In other words, you would not write: 0, 1, 2, 3, …, 8, 9, 00 or 01, as neither makes sense; one would write 10. The same applies here.

The same was the case in our 5-base system above: we used `)(` to express the next step after all our digits were used, and not `((` which would be incorrect. It would be like writing 00 instead of 6.

Once you know these basic steps which apply to all x-base systems, it becomes easier to count. And you can use keep adding a leading leftmost character, and reset the rightmost character currently in use, whenever you run out of possible next numerical steps using only the length you have at the moment. Read a few times of the binary steps and look at the progression, and soon you’ll be able to count on binary, even without using fingers. If you do use fingers, remember to only use two.

So now that we have explored 10-base, 2-base (and 5-base 😉 let us look at something which may seem odd again at first glance: 16-base. How would we fit 16 possible numerical combinations into a single character? Welcome to hexadecimal, which uses letters.

Let’s do a simple count first: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F

16 Characters in total, the hexadecimal system uses A-F once it runs out of ways to express the next number in the series. Counting from one to 11 as we did previously would be moot here, as there 11 is simply expressed by ‘B’. So let’s start a bit further in the process this time:

```0: 0
1: 1
...
9: 9
10: A
...
15: F
16: 10
17: 11
```

On the left we have 10-base decimal, and on the right we have 16-base hexadecimal. So it is easier to remember, note that hexa-decimal makes us think about 6-10.

Ouch! Now we end up with `10` in 16-base hexadecimal being really worth `16` in 10-base decimal! This may be slightly confusing and one can immediately see the need to clearly understand what numerical system we are working with to avoid costly mistakes.

Many calculators in various operating systems have a developer or computer based setting which can be activated to work with different numerical systems. Some go one step further and very clearly show you what the number at hand would translate to in various other x-base numerical systems, like this great calculator included in Linux Mint 20:

Linux Mint 20 Calculator showing Decimal, Binary, Hexadecimal, Octal all at once

## Octal

Now that we have seen the previous numerical systems, it is easier to see how we may count in an 8-base system, in this case being octal, another system used in conjunction with and by computer processing systems.

In octal, we have 8 numerical characters being 0, 1, 2, …, 6, 7. Let’s count to 11 in an 8-base numerical system, starting at 7:

```7: 7
8: 10
9: 11
10: 12
11: 13
```

On the left we have 10-base decimal, and on the right we have 8-base octal.

Again we can see a slightly confusing `10` in 10-base decimal being `12` in 8-base octal.

## Why so many numerical systems?

So why are there so many different numerical systems? The reason is simple. Remember how one bit was a store to place a binary zero or one? Well, if you take 8 bits you will have one byte, and a byte is often used to expressed simple single-byte alpha-numeric characters. If you think about how 8 really is at the base of this, it should not be too far a stretch to see octal (8) fitting in to numerical systems being used on computers.

Next we have hexadecimal, which is really 2 x 8 = 16 characters. And here, we have 16 bits (or 2 bytes) represented as one single character. It all hangs closely together, and really comes into play when you consider how alpha-numeric characters are used and processed inside computer systems. For example, some special characters (like for example Japanese or Chinese characters) may require two or three bytes to store them (multi-byte).

Various numerical systems simplify the many types of data flows which happen within a computer, and depending on the flows at hand, and any matching computer algorithms selected or used, various optimizations are possible depending on what numerical system you employ. Most developing languages have, for example, highly optimized binary and potentially hexadecimal processing besides decimal processing.

## Conclusion

In this article, we dived into 2-base, 10-base, 16-base, and 8-base numerical systems, being binary (2), decimal (10), hexadecimal (16) and octal (8). We saw what sort of connections there are between these, and how to do simple counting in all of these systems.

Learning a little more about how computers work often helps, especially when it comes to make first computer programs or understanding theory. When one becomes a full time developer, by that stage all of these systems are second nature, and they are often used within actual code.

Please leave us a comment with your insights on these numerical systems! And if you’re ready to learn more interesting things, have a look at our Big Data Manipulation for Fun and Profit Part 1 article! Enjoy!