Number Systems:
Computers use a number system named binary to process and handle data. This is because it’s relatively simple to represent such a number system in electrical circuits - 1 is on (or “has a charge”); 0 is off or “doesn’t have a charge”.
All data captured from input devices must be converted into binary in order to be processed by the computer system. As a result of the fact that the binary number system has long numbers which can be a bit awkward to handle, we make use of two other number systems to make these large numbers a bit more human friendly and rather more compact. These number systems are octal(base 8) and hexadecimal (base 16). There are many other numbering systems we do not cover in this course..
Binary: base 2 (either a zero or a one) 0,1.
You should remember that it takes 8 bits to make a byte; you may often want to pad binary numbers into bytes (or nybbles [4 bits]).
Octal: base 8 (numbers between zero and seven) 0,1,2,3,4,5,6,7
Octal is quite good at compactly expressing numbers in 8 bit systems. However, as we’ve moved into systems with longer (16, 32 and 64 bit) words, hex has become the prevalent way of expressing human-readable binary; it groups neatly into bytes every two characters, and each character is a nybble. Compare dec 255, hex FF, bin 11111111 and oct 377 - all represent the same number, but some are easier than others. This gets more interesting the other side of the byte boundary: 256, 100, 100000000, 400. As numbers get bigger, we start considering other encoding systems; Base64 is quite common in email and YouTube IDs. Taken to extremes, you end up with base65536.
Hexadecimal: base 16 (zero to 15) 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F
To avoid confusion, hex numbers are sometimes written with 0x in front of them, for example 0x9DF.
To see the relative “efficiency” of the numbering systems, have a look at these values:
“There are 10 types of people in the world. Those who understand binary, and those who don’t”.
Converting between number systems:
Converting to Decimal:
In order to convert from any other number system back to decimal, all you need to do is write the number out in expanded notation and then add up all the digits.
Converting Hexadecimal to Decimal:
Let’s convert 9DF to decimal.
Step 1: Draw a table and write out the number you want to convert like this:
Step 2: Write in all the exponents (because this is hexadecimal we will use exponents of 16):
Step 3: Work out the values of these exponents, with practise you will be able to do this from memory but if all else fails use a calculator.
Step 4: Convert the individual hexadecimal numbers to their corresponding decimal value (we put a table above with the values in - memorize these) AND multiply these by the value of the corresponding exponents.
Then add up the resulting numbers:
9*256 + 13*16 + 15*1 becomes 2304 + 208 + 15 = 2527.
So 9DF in decimal is 2527.
For longer numbers, add more columns, remembering to increase the exponent by one each time you add a column to the left.
Converting Binary to Decimal:
We do this in exactly the same way as hexadecimal BUT we start with a binary number.
Let’s convert 10101111 to decimal.
We start off with the same table as we used for Hexadecimal To Binary:
Then we perform the same steps as with hexadecimal to binary but using exponents of 2 instead.
Which brings us to 128+0+32+0+8+4+2+1 = 175.
Another Method. Some people don’t like mental arithmetic. You can skip a step if you remember the binary progression of numbers. Draw a table with as many columns as you have binary digits:
Starting from the right, fill in the binary progression in the top row - each is simply a doubling of the previous value, a convenient side-effect of base 2 exponents. Without much practice at all, you’ll soon memorise the values. (Computer RAM tends to follow the same pattern, for perhaps obvious reasons.)
Fill in the binary numbers in the row below.
Wherever you see a binary one, add up that number from the cell above; wherever you see a zero, ignore it; in the example above, it leaves this simple sum:
128+32+8+4+2+1=175
Converting From Decimal to Binary or Hexadecimal:
There are various methods for converting from Decimal to other number systems. What follows is the easiest, but not very efficient, method.
Converting From Decimal To Hexadecimal
Get your number and write it down (I’ve used a table for this but it is not necessary). Now we will repeatedly divide this number by 16 (hexadecimal is base 16) and write down the whole number in the first column, discarding the numbers after the decimal point, and write the remainder (mod) in the second column until we get to 0. I have included the calculations I used to work out the remainder in the second column.
Write the numbers out from bottom to top : 9 13 15; work out the hex letter or number for that: 9=9; 13= D; 15=F (see the . Therefore, the hex value for de 2527 is 0x9DF. We worked out the hex number using the following table (memorize it):
Converting From Decimal To Binary
Let’s convert 175 to binary.
We will use the same method we did when we worked out decimal to hexadecimal BUT instead of using 16 we will use 2, because binary is base 2.
Now write the remainder out from bottom to top and we get:
10101111
Another Method Again, you can also use a lookup table to do this, in a way:
175
Create a lookup table like the previous one, remembering your binary progression.
If the binary number equivalent can be subtracted from your decimal number, put a one in the Bin row below; if it can’t, fill in 0 skip the subtraction and try the next row until you get to zero. Fill in any remaining zeros. Your binary number magically appears in the Bin row. Follow through the example above and you’ll quickly understand.
Converting from Binary to Hex and visa versa:
The easiest way to do this is to memorize the following lookup table:
A pattern to memorize this is to look at the patterns for each binary digit going down the column.
The least significant bit (far right) has a pattern of 0101010101010101.
The next bit has 0011001100110011.
The third bit has 0000111100001111.
The last bit has 0000000011111111.
The way I usually write this out is to start on the far left and put in eight 0s and eight 1s, then four 0s and four 1s (repeated twice), then two 0s and two 1s (repeated four times) and finally alternate 0 and 1 until I’ve filled up the rows for the last bit.
For those that find the Hex conversion hard, this should clue you into another way to convert between Dec and Hex - convert decimal to binary first, and then read off the hex digits in blocks of 4 bits (and vice versa).
Binary to Hexadecimal:
Let’s say we want to convert 1101101 to hexadecimal.
Step 1: pad (from the front!) the binary number with 0s until we have groups of four digits (4 bits, or a nybble - half a byte!). In this case we have 7 digits. If we pad with a 0 on the beginning then we get 01101101. This can then be split up into groups of 4.
Step 2: Split up the number into groups of 4. With our example we get 0110 and 1101.
Step 3: take each group of four and match it to the lookup table we made earlier. 0110 corresponds to 6 and 1101 corresponds to D.
So our number is 6D.
Hexadecimal to Binary:
Now let’s take the Hexadecimal number FEE7.
Go straight to the lookup table above. F is 1111, E is 1110, E is 1110 again and 7 is 0111.
So FEE7 in binary is 1111 1110 1110 0111.
*need to add conversions for octals
At this stage, you may also want to understand binary arithmetic.