The Department of Computer Science & Engineering |
CSE 111: Great Ideas in Computer Science
|
In this example we will convert the number 200 to binary, octal, and hexadecimal, and back again. The concepts used here are introduced in my CSE111 notes.
Converting 200 to binary (base 2) and back again:
We will convert to binary using the "division method." We will start by dividing 200 by the base (in this case 2) and writing down the remainder (use long division!) in the rightmost numerical position (in this case, the one's spot). We will continue this cycle as in the table below:
Calculation Step | Explanation | Current Result |
---|---|---|
200/2 = 100r0 | As described above. | 0 |
100/2 = 50r0 | We continue by dividing 100 (the result from above) by 2, and writing the remainder to the left of the one found in the above step. | 00 |
50/2 = 25r0 | Continue as above. | 000 |
25/2 = 12r1 | 1000 |
|
12/2 = 6r0 | 01000 |
|
6/2 = 3r0 | 001000 |
|
3/2 = 1r1 | 1001000 |
|
1/2 = 0r1 | Now we have reached 0, so we know we are done. | 11001000 |
Now we'll convert 11001000 back to decimal (base 10). We do this by multiplying each digit by the appropriate power of 2 (the base):
1*27 + 1*26 + 0*25 + 0*24 + 1*23 + 0*22 + 0*21 + 0*20
Remember these powers directly correspond to the location in the binary number... for example, the first '1' on the left is in the 128's spot, which is 27.
We now multiply this out and add it up:
128 + 64 + 0 + 0 + 8 + 0 + 0 + 0 = 200, and we're done.
Converting 200 to octal (base 8) and back again:
We will once again use the division method to covert 200 to octal. The only difference from the above is that we use 8 instead of 2 to divide by!
Calculation Step | Explanation | Current Result |
---|---|---|
200/8 = 25r0 | As described above. | 0 |
25/8 = 3r1 | We continue by dividing 25 (the result from above) by 8, and writing the remainder to the left of the one found in the above step. | 10 |
3/8 = 0r3 | Continue as above. Now that we are at 0, we are done. | 310 |
Now we'll convert 310 back to decimal (base 10). We do this by multiplying each digit by the appropriate power of 8 (the base):
3*82 + 1*81 + 0*80
Remember these powers directly correspond to the location in the octal number... for example, the spot the '3' occupies corresponds to 82 which is the 64s spot.
We now multiply this out and add it up:
192 + 8 + 0 = 200, and we're done.
Converting 200 to hex (base 16) and back again:
By now you should have seen the patterns of which numbers we change to convert a number between any base and decimal and back again. We will once again use the division method to covert 200 to hex. The only difference from the above is that we use 16 instead of 2 or 8 to divide by!
Calculation Step | Explanation | Current Result |
---|---|---|
200/16 = 12r8 | As described above. | 8 |
12/16 = 0r12 | We continue by dividing 12 (the result from above) by 16, and writing the remainder to the left of the one found in the above step. Remember 12 corresponds to C in hex. Sincer we are at 0, we are done. | C8 |
Now we'll convert C8 back to decimal (base 10). We do this by multiplying each digit by the appropriate power of 16 (the base):
12*161 + 8*160
Remember these powers directly correspond to the location in the hex number... for example, the spot the 'C' occupies corresponds to 161 which is the 16s spot.
We now multiply this out and add it up:
192 + 8 = 200, and we're done.