Wednesday, January 28, 2015

Hexadecimal and octal number systems

                                                                                    Note:
This post assumes that you have already read the Binary Number system post, as the information given here is very similar to what is discussed there, and therefore will assume that you have a base of understanding on how base ten and two work.

By now, two number systems should be familiar, base two, and ten.  In reality there is an infinite amount of number systems.  To reaffirm the process, and to expand understanding, two more number systems should be discussed, base 8 (octal) and base 16 (hexadecimal).

Base 8: 
In base 8, as in every base, the first place value is always one.  In base ten, one of any ten numbers (0-9) can be put into this spot to represent the quantity of "one's" we have.  In binary one of two numbers (0 or 1) can be put in this spot.  In base eight, one of eight numbers can be put in this spot (0-7).  So if representing the number seven in octal, we would simply put "7" in the one's spot.  Every spot to the right in octal is the product of (8)*(the value of the place value to the left).  So the spot to the right of the "one's place" would be the "eight's place."  The next spot would be the "sixty-four's place."  This is the product of eight times the value of the place value to the left, which in this case was eight.  The next spot over would be the "five-hundred twelve's place."  This is the product of eight times the value of the place value to the left which in this case would be the "sixty-four's place."  This process could continue infinitely.  Say we wanted to represent the number 412 (base ten) in octal.  We know right off the bat that we do not have anything in the "five-hundred twelve's place" because our number is smaller than that.  So we turn to the next lowest value, the "sixty-four's place"  The question we now have to ask is how may sixty-four's do we have?  We have six. So we can write 6 in the "sixty-four's place."  What are we left with?  412-(64*6) = 28.  The next question we have is, how many eight's we have.  We can have a max of three.  28-(8*3) = 4.  That last step is simple, how many ones do we have?  The answer is four.  So to write 412 (base ten) in octal: 634 (base eight).  To convert that number back, we would do the same thing in reverse.  We would note the value that each quantity represents and multiply that times the quantity in that place.  For example, we have a four in the "one's place."  So we have four ones.  (4*1) = 4.  We have a three in the eight's place.  So we have three eight's.  (3*8) = 24.  Finally, there is a six in the "sixty-four's place."  (6*64) = 384.  The last step is to add all of those products together.  (4*1)+(3*8)+(6*64) = 4+24+384 = 412.

Base 16
Base sixteen works the exact same way, but instead of increments of two, eight or ten, it goes up in increments of sixteen.  Also note that a difference is that now there are sixteen values we can use (0-15).  Now, a problem with base 16, is that there are values we can use that should take up two place values (11, 12, 13,14, and 15.)  Therefore we use letters in their place.  A = 10, B = 11, C = 12, D = 13, E= 14, and F = 15.  The first place value, as any, is the one's place.  Then it goes up by multiplying each previous value by 16.  So, from left to right the values of the places are: 1, 16, 256, 4096, 65536, ect. So, if you were to start with, say D2, and you wanted to convert that to base ten, you would start by saying that you have 2, one's.  The next number (or in this case a letter) represents the number of sixteens.  D = 13, so you have thirteen, sixteens.  To calculate the number in base ten, you would add the products.  (13*16)+(2*1) = (208)+(2) = (212).  If you started with the 210, and you wanted that in hex., you would ask yourself how many sixteens you have.  You would find that you have 13.  Then you would find how many ones are left, which in this case is 2.  You would write, D2.

As it turns out there is a very convenient way of converting hexadecimal to binary and vice versa.  Each place value in binary is called a bit. Each letter/number in hexadecimal is equal to four bits, or one nibble in binary.  Say you have one nibble of binary, for example 1011, and you want to convert this to hex.  All you need to do is convert this nibble as you would of you were converting it to base ten.  In this case, the result would be eleven.  But since you want this in hex, you would write this as B.  But what if you started with 10101100?  Before, it was simpler because the binary number was already a nibble, so we skipped the first step.  Now we have eight bits, or two nibbles, so we want to break them up for now.  1010 and 1100.  Now convert each one individually as before.  1010 is really ten, written as A, and 1100 is twelve, written as C.  Now put the two results in order that the nibbles were, so AC.  Now say we wanted to convert this back.  Keep in mind that each place value in hex is four place values in binary, so we can use four spots to fill in to get ten.  The combination needed to make ten it 1010.  C is twelve.  Again, we can use four place values in binary to get this number.  The result would be 1100.  Now put the two of them together.  10101100.
The same can be done for binary to octal, and octal back.  In this case, however, the binary numbers would be grouped into groups of three, and each place value in octal is equal to three bits of binary. 

 Sometimes converting long sequences of binary to decimal can be a major pain.  Take for instance the sequence 10110000111010110101.  To convert this directly to base ten would be a complete waste of time.  This could be converted with much greater ease into hex.  Simply group them into nibbles and convert.  100111010110011010001 could be converted to octal by grouping the bits into groups of three and converting.  From there you could convert the octal, or hex result to decimal, but many programmers leave it as is.  Hex is also the number system of choice when dealing with colors because only three symbols are required to make any color.


Check out these sources for more info!

http://thestarman.pcministry.com/asm/hexawhat.html
http://mathworld.wolfram.com/Hexadecimal.html
http://www.wikihow.com/Understand-Hexadecimal



No comments:

Post a Comment