# Binary, Octal, and Hex

Okay, let’s talk about some other
positional number systems. To review, both decimal and binary number systems are
positional number systems, which means that the value of any digit inside a
number depends on its place in the number. So the first five in this number
545 represents 500, the second 5 resents five.
Similarly in binary the first one represents four, and the second one
represents one. Now the base of the number system determines how many digits
we have and the place values. So for example, in base ten decimal there’s ten
digits and the place values are all powers of ten. In base two binary the
digits are 0 and 1 and their place values are all powers of 2; 1, 2, 4, 8, 16,
and so forth. Binary numbers are great but it’s hard to read them especially
when they’re large. For example, here’s 1 million. There must be a better way to
represent 1 million and indeed there is. One common base that computers use is
based eight numbers which are called octal. Base eight numbers have eight
digits 0 through 7, and their place values are all powers of 8; one, eight,
64, or 512, and so forth. One really nice property of octal
numbers is that all of their digits 0 through 7 can be represented in three
bits, 0 0 0 to 1 1 1, and this gives us a nice way to convert binary into octave.
So here’s a simple algorithm you can use. Step 1, break the binary number into
groups of 3 bits working right to left. So I’ve taken 1 million here and broken
it into groups of 3 bits looking right to left, the last group on the left side
any two bits in it. Step 2, simply write the octal digit for the 3
bit under each 3 bit quantity. So this is a 1, this is a 1, this is a 4, this is a 6,
this is a 3. And then step 3 just read off that number so
three-six-four-one-one-zero-zero. There’s a much more economical way than the
binary representation. Another base that is used frequently in computer science is base 16. That has 16 digits, 0 through F, but we use our standard digits for the
first 10 digits and then A-F for 10, 11, 12, ,14, 15. The place values for
hexadecimal numbers are all powers of 16 so 1, 16, 256, and so forth. And base 16
numbers have the nice property that all their 16 digits can be represented in
four bits. So they go from 0 0 0 0 to 1 1 1 1, and that gives us a similarly nice
algorithm for converting binary to hexadecimal. So step one, write the number
up but this time into groups of four. This each, by the way a 4-bit group is called
a nibble, so I’ve taken 1 million and broken it right-to-left into groups of
four and it comes out into five roots of four. Step two, simply write the
hexadecimal digit for each four bit nibble. In step three, read out that
number as the hexadecimal equivalent of the binary number so F-4-2-4-0
is another way to represent 1 million. And the really nice part about
hexadecimal and octal is that it’s very easy to go in the other direction to go
from hex to binary. So do so, you just have to sort of reverse the algorithm. So
write down the hexadecimal number and under each of its digits write the 4-bit
value for that digit so F is 1 1 1 1, 4: 0 1 0 0, and so forth. Then the resulting
number is our original number 1 million in binary. Let’s finish up this lesson by
thinking about abstraction for a moment. We’ve looked at several number systems. Perhaps,
you can see an abstract pattern that they all have in common. They’re all
positional number systems, which means that their bases determine the number of
digits they have. They also determine the place values,
which is simply powers of the base. And then to calculate the value of any digit
you multiply it times place value. And to calculate the numbers value you add up
the digits. This pattern works for any number system in any base. For
example the same four digits one one zero one written in four different
number systems, and in each case we can figure out the value in that number
system by multiplying 1 times the value of the place. So 1 1 0 1 in decimal is 1
+ 100 + 1000, and binary 1 plus 4 plus 8 in octal 1 plus 64 plus 512 ,in hex 1
plus 256 plus 409. Now all of these systems decimal, binary, octal, hex, these
are all abstractions of course but their abstractions that have a pattern in
common. So that common pattern, which is the positional number system pattern, is
even more abstract once you know it you can figure out the values of numbers
written in any number system. And to leave you with a challenge
think about the quinary number system which is base 5 and how would you
translate 1 1 0 1 into its value assuming that it’s written as a base 5 number.
We’ll leave you with that.