Number Bases: How binary, hex, and other number systems work
Because of how computers work at a base electronic level, it's common to find code that makes use of numbers that are written differently than the decimal system that most people are used to. In this module, we'll first dissect the base-10 (decimal) system that we use every day, then apply what we've learned to change to other number systems, including binary and hexadecimal. Finally, we'll briefly talk about why almost all modern computers use a binary number system to perform calculations.
The numbers that we use every day for things like prices, math, banking, how many pizzas to get for a party, etc are in a system called base10, or decimal. We call it that because each 'column', or place, is associated with a power of 10. The system also has a radix of 10, meaning that each column can hold 1 of 10 possible digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, or 9.
The digit in each place is multiplied by a power of 10, and then when the
products are added together, the result is the number written in base10. The
column is multiplied by
, which is just
column is multiplied by
column is multiplied
, and so on. It's a different way of looking at the numbers
we use every day, but it hopefully gives you some insight into how and why they
are constructed the way that they are.
Note: The subscript 10 is used to denote the base of the number. We don't include this in every day math because it's assumed that unless specified, numbers are already written in base10.
It's a pretty straight-forward process for getting bigger and bigger numbers- just increase the power of 10 of the multiplier. But what about numbers smaller than 1?
Let's look at
as an example.
Thinking in the context of number bases, we can see that the decimal point
actually specifies where our
column is. When moving to the right
of the decimal point, the powers of 10 simply continue to decrease:
If you've never experienced numbers in different bases before, hopefully this is some interesting insight into the number system we use every day. Now that we've figured out how our decimal system works as written, it should be an easy task to apply this to other number bases.
In the previous block, we broke down our every day base10 system into columns, multiplied by a power of 10, then added together. With this method, we can build systems using any number for a base by just changing the powers. For example, let's examine a ternary (base3) numbering system. In a base3 system, each column represents a power of 3 (rather than 10, as in the decimal system). Also, the system has a radix of 3, meaning each column can take on only 3 different values: 0, 1, or 2.
ex. Write the base3 number
in the decimal (base10) system:
(the right-hand-side of the equations are all in base10)
So the number
, a number written using
as a base, is the
in the base10 system.
Converting a non-decimal system to base10 is straight-forward:
In the base10 system (where we're used to doing our math), multiply each digit from the number in the alternate base (base3 in the above example) by the power of the base that's appropriate for that column.
Add the products together (still in base10). The result is the number that was originally written in an alternate base, now written in base10.
The exact same process for converting to base10 can be used to convert from base10 to another base. However, all of the math on the right-hand-side ( multiplication and addition) would need to be done in the alternate base system for it to work. So while it's possible... our brains are used to doing math in base10, so usually use subtraction instead. Here's an abstract step-by-step (we'll take a look at an example right after so it makes more sense):
Find the highest
multiple * powerof the base that will fit into the base10 number.
Write the alternate base number in the correct column, leaving blanks to the right of it (to fill in later steps).
Subtract the amount equivalent to the alternate base number from your current base10 number. This is now your remainder.
Jump back to step 1, using this remainder instead of your original number.
Repeat steps 1-4 until your remainder is 0.
ex. Write the decimal number
in a base4 system.
Find the highest
mulitple * power
that will fit in
. In base10,
, which is too high. So we'll start
for our multiple- so we get
because it's a base4 system, the radix is 4, meaning that each column
can only take on 4 possible values: 0, 1, 2, 3.
We'll place a
column, which is the 4th one to the left.
The remaining places we'll leave blank
Now we can subtract
from our original
, since we're accounting
in the 4th column of our new base4 number.
Using our remainder of
, jump back to step 1 and continue the
process. The next column is
, and the highest multiplier of
that will fit into
we'll put a
Subtract our newest multiplier from our working remainder:
So our new remainder is 11. Repeating the process again, our next column's
power of 4 is
. The highest multiplier is 2 (
Calculate our new remainder:
With the new remainder of 3, and the final column being essentially a 1's column,
we can fill in the final column with just
written in base4 is
It might seem like a lot of work, but luckily, most of a programmer's work in other number systems involves only a handful of other bases. The more you work in these other base systems, the easier it is to convert back and forth. The two most common systems that you'll see while programming are binary, a base2 system, and hexadecimal, a base16 system. Other common systems are base8 (octal) and base64.
Binary is another name for a base2 number system, meaning each column represents
a power of 2, and the radix (the number of unique digits in the system)
is 2. So each column/place in a binary number can have one of two values:
. In programming, binary numbers (binary literals) are often represented
by putting a
is the equivalent of
Let's start by converting a binary number to decimal, using the same technique used earlier:
ex. Write the binary number
in decimal (base10):
Recall that the right hand side (where we do our math) is in base 10
We found that
is equal to
in decimal. Let's try going the other
ex. Write the decimal number
The largest power of 2 that fits in to 116 is 6 (
), so we'll
start in the 7th column (the 1st column represents
The next power of 2 that fits in to our remainder of 52 is
The next power of 2 that fits in to our remainder of 20 is
Now, we've reached a point where the next power of 2,
big to fit into our remainder. We fill it's place with a 0 and move on to the
The next power fits right in (
It also leaves us with
for a remainder. Since the only multiple of 0 is
0, we can be pretty confident that there are no remaining powers of two that
will fit into our answer. Thus, we can fill the rest of the blanks with
Hexadecimal, or just 'hex', is the most common number system other than decimal that you will see in programming. Hexadecimal numbers are written in base16; having a radix of 16 allows for larger numbers to be written with less symbols, but also means that you need at least 16 different symbols to write these numbers. Using arabic numerals, we only have 10 digits, so we fill the remaining symbols in with the letters A-F.
That means that each column in a hexadecimal number can take on a value between 0 and F, with the following equivalence:
In programming, or computing in general, hex numbers can be written in a number
of ways. Most commonly, they're hexadecimal literals are written as
with the leading
signifying that the following is a base16 number. The
may also be replaced with a slash, or other character, but generally
there is an
that lets you know the number is in hex. Because hexadecimal is
so common in computing, oftentimes there won't be any sign at all that a number
is written in hex- but you can usually tell from the context or the mix of
numbers and letters A-F.
Let's do a quick conversion, hexadecimal to decimal as an example:
(right-hand-side in base10)
Notice that we used 1 character less when writing the number in hexadecimal versus decimal. If we had a lot of numbers to store (millions or billions) as text in something like a document, it would be much more space efficient to store them as hex rather than decimal.
The relationship between base2 (binary) and base16 (hex) makes it extremely simple to convert back and forth between the two. Each column in hexadecimal can be comprised of 4 columns of binary and vice-versa. Converting between the two is as easy knowing the numbers 0-15 in both binary and hex.
We'll just look at each column one at a time, and use the chart to convert:
After we have the binary number for each hex digit, write them in order to obtain the complete binary equivalent. Note that it's standard to put a separator (in this case, a space) every 4 digits in binary to help with readability:
To convert a binary number to hex, the same strategy applies, but in reverse. The binary number is broken up into groups of 4, and then the equivalent hex value is substituted:
First note that there are only 7 digits in the binary number. To be able to
separate it into 2 groups of 4 digits, we can simply add a
out front, then
do the conversion:
If you've been around computers enough, you may have noticed that a lot of numbers occur in powers of 2: you might have a 256 gigabyte SSD in your computer, or 2048 bytes of SRAM on an Arduino's microcontroller. Binary, and thus powers of 2, is the base number system with which all modern computers operate. In the final block, we'll discuss the very basics of why that is.
It's common knowledge that nearly all modern computers operate with binary,
but the reasons for that aren't as clear without looking at the fundamental
electrical components of a computer. Computers are machines of logic, where
everything is always in one of two states:
. It's a pretty
simple leap to say that we can represent
with the binary
- they're both binary systems, just using different
symbols for each 'column'.
The base electrical component used to build the logic circuits of a computer
is the transistor, which is essentially an on/off switch that can be
controlled electronically (similar to a relay).
is of course
another binary system, so the physical state of an electrical circuit- that is,
a switch being
is conducting electricity and a switch being
conducting- can be used to represent things like
When transistors connected together and arranged in particular ways, we can form logic gates, leading to basic logic blocks that can store information, do math, and eventually guide a ship to the moon.
Even though computers operate with individual
's (bits), it's not
efficient for human designers or operators to work at this level. Instead, the
smallest useable piece of information we can usually read or write to on a
computer is larger- typically a block of 8 bits (an octet) at the smallest.
With modern computers, a block of 8 bits is referred to as a byte.
We saw in the previous block that it was really convenient to store information using number systems with a higher base than 10. We also observed that hexadecimal fits great with binary- as long as it's arranged in blocks of 4 digits. In computing, a block of 4 binary digits is commonly referred to as a nibble (...a small 'byte'). With each hexadecimal digit equal to a nibble (a series of 4 bits), we can write a single byte (the base unit of information on a computer, made up of 2 nibbles or 8 bits), using just two hexadecimal digits.
After you've read that last sentence 3 or 4 times and the meaning has become clear, hopefully you'll come to see why it's often convenient for programmers to write out values in hexadecimal in their code. It's especially useful when not referencing an actual number, but instead a series of bits. For example, when controlling a string of LEDs with an Arduino, you might have the following sequence:
On/off is just 1's and 0's to a computer, so the same thing written in binary would be:
Which is just a number. In base10 (decimal), that number is 22. The phrase "set the LED string to 22" has no physical meaning though, so you'll often find 'numbers' like this written out using either hex or binary literals:
1 2 3 4 5
led_string = b00010110 or led_string = 0x16
Note: A lot of languages don't have/allow binary literals, so it's much more common to see and use hexadecimal. As a rule of thumb, always use hex values in your code, and if you feel it's necessary, you can write the binary values in nearby comments.
base, radix: In mathematical numeral systems, the radix or base is the number of unique digits, including zero, used to represent numbers in a positional numeral system. For example, for the decimal system (the most common system in use today) the radix is ten, because it uses the ten digits from 0 through 9 1.
binary: A number system with radix 2, commonly used digital logic circuitry.
hexadecimal: A number system with radix 16, widely used by computer programmers as a human-friendly representation of binary values.
transistor: An electrical component used as an electrically-controlled switch in digital logic circuits 2.
bit: A logic 1 or 0 (True or False) in computer circuitry.
byte: A combination of 8 bits.
nibble: Half of a byte, or 4 bits.
Also, if you're interested in playing around with digital logic and enjoying some Minecraft at the same time, Redstone circuits are a great way to learn and practice the basics. If you've ever seen videos of people building counters, adders, and mini-computers in Minecraft, binary math and basic digital logic are where they started.