Yorkville High School Computer Science Department
Yorkville High School Computer Science Department on Facebook  Yorkville High School Computer Science Department Twitter Feed  Yorkville High School Computer Science Department on Instagram

Yorkville High School Computer Science

ASSIGNMENTS: No Current Assignments

AP Computer Science :: Lessons :: Number Base Conversions

Barron's AP Computer Science

Chapter 2:
Pages 72 - 72


Fundamentals of Java
Chapter 1
Pages 10 - 12
Appendix E

Binary Numbers

Normally, we represent numbers in decimal notation, also known as base 10. Decimal notation uses 10 digits (0-9) to represent numbers. We use this notation because it matches the number of fingers we have on our hands. Binary notation, or base 2, uses 2 digits (0-1) and is used by computers because of the on/off nature of an electric current. Below are the first 32 numbers (0-31) displayed as decimal and binary. You may sometimes find it easier to display leading 0s when using binary so the numbers below are displayed with leading 0s. Those leading 0s are not necessary so feel free to leave them off if you can understand the numbers without them.

Decimal Binary
0 00000
1 00001
2 00010
3 00011
4 00100
5 00101
6 00110
7 00111
8 01000
9 01001
10 01010
11 01011
12 01100
13 01101
14 01110
15 01111
16 10000
17 10001
18 10010
19 10011
20 10100
21 10101
22 10110
23 10111
24 11000
25 11001
26 11010
27 11011
28 11100
29 11101
30 11110
31 11111

Octal Numbers

Octal File Permissions

Octal notation, or base 8, uses 8 digits (0-7) and is occasionally used in computers. Unix file permissions use Octal, and it used to be helpful on older computer systems that used 24- or 32-bit words. Octal could be used as a binary abbreviation on these computers because an octal digit could cover exactly three binary digits since 0 through 7 covers 000 through 111 in binary. Modern computers, however, use 16-, 32-, or 64-bit words, which are then divided into 8-bit bytes. Three octal digits would be required for every byte, but the third octal digits would only cover 2 binary digits, which wastes space. This is the main reason why octal has fallen in use. Below is a table showing decimal, binary, and octal numbers 0 through 31.

Decimal Binary Octal
0 00000 00
1 00001 01
2 00010 02
3 00011 03
4 00100 04
5 00101 05
6 00110 06
7 00111 07
8 01000 10
9 01001 11
10 01010 12
11 01011 13
12 01100 14
13 01101 15
14 01110 16
15 01111 17
16 10000 20
17 10001 21
18 10010 22
19 10011 23
20 10100 24
21 10101 25
22 10110 26
23 10111 27
24 11000 30
25 11001 31
26 11010 32
27 11011 33
28 11100 34
29 11101 35
30 11110 36
31 11111 37

Hexadecimal Numbers

Photoshop Hexadecimal Color Picker

Hexadecimal notation, or base 16, uses 16 digits to represent numbers. Since we only have 10 numbers (0-9) the letters A-F are used to represent 10 through 15. Each hexadecimal digit can represent 4 binary digits (also known as a nibble) so it is much better than octal at shortening binary. You will find hexadecimal numbers used to represent colors in image-editing programs or on the web and you will also find them used in almost any programming language to some degree, especially for assembly languages. This final table shows all four number bases including hexadecimal.

Decimal Binary Octal Hexadecimal
0 00000 00 00
1 00001 01 01
2 00010 02 02
3 00011 03 03
4 00100 04 04
5 00101 05 05
6 00110 06 06
7 00111 07 07
8 01000 10 08
9 01001 11 09
10 01010 12 0A
11 01011 13 0B
12 01100 14 0C
13 01101 15 0D
14 01110 16 0E
15 01111 17 0F
16 10000 20 10
17 10001 21 11
18 10010 22 12
19 10011 23 13
20 10100 24 14
21 10101 25 15
22 10110 26 16
23 10111 27 17
24 11000 30 18
25 11001 31 19
26 11010 32 1A
27 11011 33 1B
28 11100 34 1C
29 11101 35 1D
30 11110 36 1E
31 11111 37 1F

Base Conversion To Decimal

Converting from binary to decimal is fairly easy. It makes more sense, though, when you realize what numbers actually stand for. Take the decimal number 4302. That number means there are 4 thousands, 3 hundreds, 0 tens, and 2 ones in the number. You could express it like this:

(4 * 1000) + (3 * 100) + (0 * 10) + (2 * 1)

or

(4 * 103) + (3 * 102) + (0 * 101) + (2 * 100)

Either one of the above formulas will equal 4302, but the second is a little easier to write because we are using exponential notation. Also, you may have realized that it can be difficult to tell what number base a number is in just by looking at it. For example, 4302 could be decimal, octal, or hexadecimal. For this reason numbers will have a subscript after them specifying the base if the base is not obvious. If there is no subscript you can assume decimal notation. So the number 4302 would be shown as 4302DEC or just 4302 to indicate it is in base 10.

Converting from binary to decimal uses this same principle. Let's try converting 100101BIN to decimal. The difference compared to what we did in base 10 was we will be using a 2 instead of a 10 in exponential notation.

(1 * 25) + (1 * 22) + (1 * 20)
= 32 + 4 + 1
= 37

Notice that we did not include any of the bits that were equal to 0 since they would evaluate to 0 anyway. If this is confusing for you then simply include them in the formula.

Converting from octal to decimal works the same way. Let's convert 230OCT to decimal.

(2 * 82) + (3 * 81)
= (2 * 64) + (3 * 8)
= 128 + 24
= 152

When using a base other than binary you need to remember to multiply the number in front of the exponent to get the correct result. Finally, let's convert a number in hexadecimal format to decimal. We will convert 2C03HEX to decimal.

(2 * 163) + (12 * 162) + (3 * 160)
= (2 * 4096) + (12 * 256) + (3 * 1)
= 8192 + 3072 + 3
= 11267

The key difference with hexadecimal conversion is you need to remember what the letters stand for. C stands for 12, in this case, so we substituted 12 in the formula.

Base Conversion From Decimal

Converting to another base from decimal is a little trickier than what we just accomplished, but if you're a fan of long division you'll feel right at home. To convert decimal to any other base you simply need to divide the decimal number by the new base number. The remainder each time will be the digits of the new number from right to left. You keep dividing until there is nothing left. Let's see any example by converting 71 to binary. Start at the bottom of the divisions below and move up.

Binary Number: 1000111

     0 R 1

2)  1 R 0

2)  2 R 0

2)  4 R 0

2)  8 R 1

2)17 R 1

2)35 R 1

2)71

The answer to the above problem would be 1000111BIN. You can check your answer by converting back to decimal if you need to.

We won't do an octal conversion since that will be exactly the same as the above except you would substitute an 8 for the 2, but let's try to convert 8435 to hexadecimal.

Hexadecimal Number: 20F3

           0 R 2

16)      2 R 0

16)    32 R 15

16)  527 R 3

16)8435

The answer to the above problem would be 20F3HEX since 15 equals F in hexadecimal. For practice converting between different number bases you can pick any number and use the number base converter at Cut The Knot to check your answer.

Yorkville High School Computer Science Department on Facebook Yorkville High School Computer Science Department Twitter Feed Yorkville High School Computer Science Department on Instagram