cstutoringcenter.com logo
E-mail:Password:

Become a premier member today to gain access to exclusive
member benefits! Just $5.00 to join FOR LIFE!







***This tutorial covers basic conversions for number systems. Floating point numbers, sign-extended numbers, fixed point numbers and any of the "compliment" methods will not be mentioned. This section is just a basic understanding of conversions and representations.***

There are many number systems in the world of computer science and mathematics. Some systems include the decimal system (base ten), octal system (base eight), binary system (base two) and hexadecimal system (base sixteen).

The below chart can certainly come in handy as these are most commonly seen when converting numbers. The table is representing the first 11 powers of 2 which includes 2 to the 0th power.

Power	Result	Binary form
20	1	1
21	2	10
22	4	100
23	8	1000
24	16	10000
25	32	100000
26	64	1000000
27	128	10000000
28	256	100000000
29	512	1000000000
210	1024	10000000000

Representations

Number systems each have different representations. For example, each number we see written out is implied to be in decimal unless otherwise stated. For example, we could write the following numbers in decimal this way:

9	(9)10
10	(10)10
11	(11)10
etc...

Each number system has what is called a base. As mentioned earlier, decimal is a system of base 10; octal is a system of base 8; binary is a system of base 2 while hexadecimal is a system of base 16. The above numbers in the parenthesis show the base representation for decimal numbers. Either way you write them is acceptable but it is much more conventional to write them without the parenthesis and the base.

The base is also the number of digits or characters used to represent numbers in the system. Below is a chart of the digits or characters each number system uses:

Binary: 0|1

Octal: 0|1|2|3|4|5|6|7

Decimal: 0|1|2|3|4|5|6|7|8|9

Hexadecimal: 0|1|2|3|4|5|6|7|8|9|A|B|C|D|E|F
	where A = 10; B = 11; C = 12; D = 13; E = 14; F = 15

So in turn, the range of digits or characters for each system is: 0 ≤ d < base

Here is a chart showing some decimal numbers and their equivalent representations in each of the mentioned number systems.

Decimal	Binary	Octal	Hexadecimal
0	0	0	0
1	1	1	1
2	10	2	2
3	11	3	3
4	100	4	4
5	101	5	5
6	110	6	6
7	111	7	7
8	1000	10	8
9	1001	11	9
10	1010	12	A
11	1011	13	B
12	1100	14	C
13	1101	15	D
14	1110	16	E
15	1111	17	F

Again, each can be written in the base representation. One example is:

(4)10 = (100)2 = (4)8 = (4)16

Conversions

What happens when someone only knows how to read numbers in a certain way. Most everyone knows how to read decimal numbers. So say we are given a binary number or a hexadecimal number that a person cannot read and understand. We can convert that given number into the appropriate system using a set of rules.

The first type of conversion that is easiest to learn is a binary number to a decimal number.

Binary to Decimal

To take a small example, the binary representation of the decimal number 9 is: (1001)2

The algorithm to go about the conversion is:

1.   Start at the rightmost bit.
2.   Take that bit and multiply by 2n where n is the current position 
     beginning at 0 and increasing by 1 each time.  This represents a 
     power of two.
3.   Sum each terms product until all bits have been used.

So using the above methods, here is the resulting conversion to decimal above:

1 * 23 + 0 * 22 + 0 * 21 + 1 * 20
8 + 0 + 0 + 1 = (9)10

A shortcut is to avoid counting the 0 bits in the sum. It saves space and time when converting but also remember that you need to increase the counter for the converting otherwise the number will not be correct.

Let's look at another example converting the below binary number: (101101001)2

Using the conversion steps above and skipping the 0 bits, here is the conversion:

1 * 28 + 1 * 26 + 1 * 25 + 1 * 23 + 1 * 20
256 + 64 + 32 + 8 + 1 = (361)10

Decimal to Binary

A more annoying and longer way of converting numbers is the decimal system to binary system. The best way of doing this is to follow these steps:

1.	Divide the decimal number by 2.
2.	Take the remainder and record it on the side.
3.	REPEAT UNTIL the decimal number cannot be divided 
        into anymore.
4.	With the bits, record them in order from right to left 
        as that will be the number in base two.

The example here with the decimal number 8 is a good way to start:

8 / 2 = 4	0
4 / 2 = 2	0
2 / 2 = 1	0
1 / 2 = 0	1

Answer: (1000)2

To check the result, use the previous method of converting from binary to decimal:

1 * 23 = (8)10

Let’s see another example using (125)10


125 / 2 = 62		1
62 / 2 = 31		0
31 / 2 = 15		1
15 / 2 = 7		1
7 / 2 = 3		1
3 / 2 = 1		1
1 / 2 = 0		1

Answer: (1111101)2

To check the result:

1 * 26 + 1 * 25 + 1 * 24 + 1 * 23 + 1 * 22 + 1 * 20
64 + 32 + 16 + 8 + 4 + 1 = (125)10

Binary to Hexadecimal

Another easy conversion to do is binary to hexadecimal. The hexadecimal number system uses the digits 0 to 9 and A,B,C,D,E,F. And since the hexadecimal system is a power of 2 (24), we can take a binary number in groups of 4 and use the appropriate hexadecimal digit in it’s place.

The steps to doing so are simple. Begin at the rightmost 4 bits. If there are not 4 bits, pad 0s to the left until you hit 4. Repeat the steps until all groups have been converted.

An example is below:

Take the binary number (1000101). Using the above steps, here is the conversion:

0100 | 0101 	Note that we needed to pad a 0 to the left.
4 | 5

Answer: (45)16

Take the binary number (11101011). Using the same steps, here is the conversion:

1110 | 1011
E | B

Answer: (EB)16

Hexadecimal to Binary

This conversion is also simplistic. Given a hexadecimal number, simply convert each digit to it’s binary equivalent. Then, combine each 4 bit binary number and that is the resulting answer.

Take the hexadecimal number (A2F)16 and convert it to it’s binary form:

A | 2 | F
1010 | 0010 | 1111

Answer: (1010 0010 1111)2

Take the hexadecimal number (55)16 and convert it to it’s binary form:

5 | 5
0101 | 0101

Answer: (1010101)2

Binary to Octal

Since the octal system is again a power of two (23), we can take group the bits into groups of 3 and represent each group as an octal digit. The steps are the same for the binary to hexadecimal conversions except we are dealing with the octal base now.

Take the binary number (10011)2 and convert it to octal:

010 | 011
2 | 3

Answer: (23)8

Take the binary number (10010101011)2 and convert it to octal:

010 | 010 | 101 | 011
2 | 2 | 5 | 3

Answer: (2253)8

Octal to Binary

To go from octal to binary, simply reverse the above algorithm and represent each octal digit in it’s three bit binary form.

Take the octal number (742)8 and convert it to binary:

7 | 4 | 2
111 | 100 | 010

Answer: (111100010)2

Take the octal number (1234567)8 and convert it to binary:

1 | 2 | 3 | 4 | 5 | 6 | 7 
001 | 010 | 011 | 100 | 101 | 110 | 111

Answer: (001010011100101110111)2

Decimal to Octal

Similar to the decimal to binary method, you must divide each number by 8 and record the remainder. This continues until the number cannot be divided into anymore.

Take the decimal number (18)10 and convert it to octal:

18 / 8 = 2	2
2 / 8 = 0	2

Answer (22)8

Take the decimal number (46)10 and convert it to octal:

46 / 8 = 5	6
5 / 8 = 0	5

Answer: (56)8

Octal to Decimal

Similar to the binary to decimal method, simply take each digit in the octal base and multiply by the power of 8.

Take the octal number (764)8 and convert it to decimal:

7 * 82 + 6 * 81 + 4 * 80
448 + 48 + 4 = 500

Answer: (500)10

Take the octal number (5771)8 and convert it to decimal:

5 * 83 + 7 * 82 + 7 * 81 + 1 * 80
2560 + 448 + 56 + 1 = 3,065

Answer: (3065)10