Welcome to RobotBox!

RobotBox is a community for robot builders to show off their projects. Add yours today.

Features of RobotBox

  • Showcase projects
  • Make new friends
  • Rate other's robots

Note: Facebook Connect temporarily disabled. Go here to login without it.

How to Understand Binary and Hexidecimal Numbers

gallamine's picture

1

The two main number systems used in robotics or electronics are hexadecimal and binary. This article will help explain what they are, why you use them, and how to understand them.

First off, what is a "number system"? It's a bit difficult to put into words, but basically it's a way of describing how many of something there are. Think of it like how different languages use different words to describe the same things. I can use words to say "there are two apples", or I could use a number system and say, "there are 2 apples," (in this case decimal and hexadecimal are the same) but it would be equally valid to say, "there are 10 apples", if I was using a binary number system. Just like there are many words in various languages to describe the tall plant in your backyard ("tree", "arbor", "baum") so there are many ways to talk about numbers. We'll cover three important ones.

Why use different systems?

When it comes to hexadecimal and binary the reasons to use them are convenience and readability. When compared to decimal (the number system we're most used to) hexidecimal is much more compact. i.e. it uses less characters. For instance, in hexidecimal (or "hex" from now on), the number "128" can be expressed as "80". While we've saved one character in this instance, the bigger the numbers get, the larger the savings. How that works will be explained further down.

In terms of convenience, many times the the numbers represent physical pins on a computer chip or microcontroller. Most output ports are divided into banks of 8 pins each. Each of those pins can be controlled "on" or "off", and it's convenient to set them all at once instead of setting them on or off one at a time. In this case we write a number to a register that represents each of the pins. Many times we show that number as binary. So, in order to set alternating pins on and off we might set the register to be "01010101" - where a "1" makes a pin on, and a "0" turns a pin off. If we wrote this number in decimal it would instead be "85", which doesn't communicate the value of each pin very well.

How do I learn to read these systems?

Now that we know that number systems are really just different languages for numbers, it's time to learn how to read them. Just as when we learn a new language, one of the first things you learn is the rules of the language. So before we can "speak" hex or binary, we need to understand a little bit about decimal.

The word "decimal" has the Latin root word, "dec" which means "ten". We also call decimal a "base 10" number system. This basically means we have ten basic "words" we use in the system. We're all extremely familiar with this words, but we might just not have thought of them as such. They are "0", "1", "2", "3", "4", "5", "6", "7", "8", and "9". "What about 10", you might ask? Well, to communicate the concept of ten, we actually use two of the words in this system, "0" and "1". Got it?

If we, instead of ten, had two words in our vocabulary, we'd have a "base 2" (or binary) system, and if we had sixteen, we'd have "base 16" (or hexidecimal) system. By using the basic vocabulary of our system, we can communicate every known number of objects by combining the basic words.

Let's look at how this is done with decimal. First off we use the concept of "places" - like the "ones' place", the "ten's place", the "hundred's place", and so on. We use this to indicate groupings of ones, tens, and so forth of objects. The number "23" has a two in the "ten's place" and three in the "one's" place. It shows we have two groupings of ten, and three groupings of ones. Two times ten equals twenty and three times one equals three. Twenty plus three equals twenty three:



2 x 10 = 20
3 x 1 = 3

20 + 3 = 23



For the number "128" we have one grouping of a hundred, two groupings of tens and eight groupings of ones.


1 x 100 = 100
2 x 10 = 20
8 x 1 = 8

100 + 20 + 8 = 128



Notice that each of the places is actually a power of our base. The one's place is ten (our base) raised to the power of zero (remember, any number raised to zero is one). The ten's place is ten raised to the power of one. The hundred's place is ten raised to the second power ... well, you get the picture.

Believe it or not, this is how you learned numbers. You just may not have realized it. We basically look at the whole group and say, "what's the largest block I can break this into using the words I have?" You subtract that block and the ask the question again. For the number nine hundred and ninety eight, we know we can't make a grouping of one thousand, so we drop down to the hundreds. We know we can break it into nine groups of one hundred, leaving us ninety eight. Ninety eight can be broken into nine groupings of ten, and so forth.


998 - (9 x 100) = 98
98 - (9 x 10) = 8
8 - (8 x 1) = 0


Ok, so that's a little complicated. Here's the takeaway message: each place is a power of our base, and each number in each place represents a block of numbers. Written out, here's what the places looks like:


10^0 = 1
10^1 = 10
10^2 = 100
10^3 = 1000
etc ...



What happens if ...

So what would happen if we changed our base? Nothing really. You just do the same procedure, but using the different base. Let's take base sixteen, for example. Our new vocabulary has to have sixteen words in it. Let's start with ones we already know, "0", "1", "2", "3", all the way to "9". So there's ten, we now need six more. Let's borrow some from the regular alphabet: "A", "B", "C", "D", "E", and "F". There, we've got sixteen!

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
the sixteen "words" in the hexidecimal vocabulary

Next thing we need to do is to figure out what the places are. Remember in base ten we figured this out by raising our base (10) to all the powers starting at 0. The first two are easy. Sixteen (our base), raised to zero is zero, sixteen raised to one is sixteen. After that it gets a little hard, and you might want to break out a calculator. Here they are for your reference:



16^0 = 1
16^1 = 16
16^2 = 256
16^3 = 4096
16^4 = 65536
etc.



In most cases it'll be rare to see hex numbers longer than four digits - or greater than decimal 65,536.

So, if we see the hex number (which I'll indicate from here on out by putting "x" in front of it), "x10". What is it in decimal? First, think about your places. That number tells us that there is one grouping of sixteen (based on the 1 in the sixteen's place), and zero groupings of ones. So that number represents "16" in decimal!

What about "x12"? Just like before we have one grouping of sixteens, but this time we have two groupings of ones, so that means we've got 16 + 2 = 18.



x12 = (1 x 16) + (2 x 1) = 18



Let's try a larger one:



x123 = (1 x 256) + (2 x 16) + (3 x 1) = 291



Did you get where the "256" came from? It's because there was a "1" in the "256's" place (the third position, or 16^3).

Now what about "xDEF"? Remember that we added some words to our vocabulary. "A" through "F" in hex now represent the decimal numbers "10" - "15". So, "D" represents "13" in decimal, "E" represents "14" in decimal" and "F" represents "15" in decimal. Using this, we can now solve the riddle:



xDEF = (13 x 256) + (14 x 16) + (15 x 1) = 3,567



Once again we multiplied the value times the place it was in, summed all the numbers, and that is our decimal equivalent. In general it is much easier to go from an alternate system to decimal, than it is to go from decimal into another system. For that, I recommend using a calculator. MS Windows (and I'm sure, others) have built in calculators that convert between decimal, binary and hex. Just be sure to turn on the "scientific" mode.

What about binary?

As you probably are figuring out, binary is approached just the same as hex.

1) Figure out the vocabulary.

Binary has two "words". "0" and "1". Simple.

2) Figure out the places.

Remember that the places are determined by the powers of the base. Binary is "base 2" system, so we have the following places:


2^0 = 1
2^1 = 2
2^2 = 4
2^3 = 8
2^4 = 16
2^5 = 32
2^6 = 64
2^7 = 128
2^8 = 256
etc.



3) Convert from the alternate system to decimal by multiplying the place holders by the place values.

Multiple each place value (listed above) by the placeholder - or the value that sits in that place. For the binary (which we indicate by putting "b" in front of) number  b1100, we do the following:


1 x 8 = 8
1 x 4 = 4
0 x 2 = 0
0 x 1 = 0

8 + 4 + 0 + 0 = 12



So, b1100 is 12 in decimal. Do you remember what it is in hex? Don't forget that we have more than enough words in hex to describe 12, so we only need one "word" - it's "C" in hex. Did you get it? Don't worry if you didn't. This stuff takes time.

Here's another binary example:


b10101010 = (1 x 256) + (0 x 128) + (1 x 64) + (0 x 32) + (1 x 16) + (0 x 8) + (1 x 4) + (0 x 2) + (1 x 1) = 170



As you can see, binary isn't nearly as compact as hex, but if we were turning on and off LEDs connected to a microcontroller, we could immediately see which were on and which were off - much easier than if we were just looking at the number "170", or "xAA".

Binary doesn't make things any shorter, but it sure does make it easy to string together a bunch of flags, or pin directions, or true/false's into a number. Many types of chips will use registers where each bit position turns on or off certain aspects of the chip. In these cases you write binary strings into the registers to control each item. Sometimes the numbers will be in hex or decimal, but you can now easily convert them to binary to understand how each bit is being set.

What does this all mean?

All this means one thing: hex and binary shouldn't be scary! In fact, you can now learn to love them. Don't get me wrong, it'll take a lot of practice, but once you've got it, you'll thank me. You won't be writing number like 32,712 anymore, but instead put x7FC8. And next time you see a binary number b110, you'll know it's really 6 in decimal.

To continue your studies, I'd suggest writing out a bunch of hex or binary numbers and trying to convert them to decimal. You can use a calculator to check your answers. Google makes it easy to convert such numbers. You can put the following into a search box and it will be converted: "0xnumber to decimal" or "0bnumber to decimal", where "number" is the hex or binary number. You can also search for "number to binary" or "number to hex" to convert the other way.

Good luck!