Professional Documents
Culture Documents
1.6.1 Decimal
We normally represent numbers in their decimal form. Numbers in decimal form are in
base 10. This means that the only digits that appear are 0-9. Here are examples of
numbers written in decimal form:
12610 (normally written as just 126)
1110 (normally written as just 11)
1.6.2 Binary
Numbers in binary form are in base 2. This means that the only legal digits are 0 and 1.
We need to write the subscript 2 to indicate that the number is a binary number. Here
are examples of numbers written in binary form:
11111102
10112
1.6.3 Octal
Numbers in octal form are in base 8. This means that the only legal digits are 0-7. We
need to write the subscript 8 to indicate that the number is an octal number. Here are
examples of numbers written in octal form:
1768
138
1.6.4 Hexadecimal
Numbers in hexadecimal form are in base 16. This means that the only legal digits are 0-
9 and the letters A-F (or a-f, lowercase or uppercase does not matter). We need to write
the subscript 16 to indicate that the number is a hexadecimal number. Here are
examples of numbers written in hexadecimal form:
7E16
B16
Hexadecimal 0 1 2 3 4 5 6 7 8 9 A B C D E F
Decimal Equivalent 1
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 5
Table 3: Hexadecimal Numbers and their Equivalence to decimal numbers
Introduction to Programming I 19
J.E.D.I
1.6.5 Conversions
For Example:
12610 = ? 2
Quotient Remainder
126 / 2 = 63 0
63 / 2 = 31 1
31 / 2 = 15 1
15 / 2 = 7 1
7 / 2 = 3 1
3 / 2 = 1 1
1 / 2 = 1
So, writing the remainders from the bottom up, we get the binary number 1111110 2
To convert a binary number to decimal, we multiply the binary digit to "2 raised to the
position of the binary number". We then add all the products to get the resulting decimal
number.
For Example:
11111102 = ? 10
Position 6 5 4 3 2 1 0
Binary
1 1 1 1 1 1 0
Digits
0 x 20 = 0
1 x 21 = 2
1 x 22 = 4
1 x 2 3= 8
1 x 24= 16
1 x 25 = 32
1 x 26 = 64
TOTAL: 126
Introduction to Programming I 20
J.E.D.I
Quotient Remainder
126 / 8 = 15 6
15 / 8 = 1 7
1/8= 1
So, writing the remainders from the bottom up, we get the octal number 176 8
Quotient Remainder
14 (equal to hex
digit E)
126 / 16 = 7
7 / 16 = 7
So, writing the remainders from the bottom up, we get the hexadecimal number 7E16
***
Converting octal or hexadecimal numbers is also the same as converting binary numbers
to decimal. To do that, we will just replace the base number 2 with 8 for Octal and 16 for
hexadecimal.
Position 2 1 0
Octal Digits 1 7 6
6 x 80 = 6
7 x 81 = 56
1 x 82 = 64
TOTAL: 126
Introduction to Programming I 21
J.E.D.I
Position 1 0
Hex Digits 7 E
14 x 160 = 14
7 x 161 = 112
TOTAL: 126
For Example:
11111102 = ? 8
0 0 1 1 1 1 1 1 0
1 7 6
Equivalent octal number
Converting octal numbers to binary is just the opposite of what is given above. Simply
convert each octal digit into its binary representation (given the table) and concatenate
them. The result is the binary representation.
Introduction to Programming I 22
J.E.D.I
Hexadecimal Binary
Digit Representation
0 0000
1 0001
2 0010
3 0011
4 0100
5 0101
6 0110
7 0111
8 1000
9 1001
A 1010
B 1011
C 1100
D 1101
E 1110
F 1111
Table 6: Hexadecimal Digits and their corresponding binary represenation
For Example:
11111102 = ? 16
0 1 1 1 1 1 1 0
7 E
Equivalent Hexadecimal
number
Converting hexadecimal numbers to binary is just the opposite of what is given above.
Simply convert each hexadecimal digit into its binary representation (given the table)
and concatenate them. The result is the binary representation.
Introduction to Programming I 23
J.E.D.I
1.7 Exercises
1.7.1 Writing Algorithms
Given the following set of tasks, create an algorithm to accomplish the following tasks.
You may write your algorithms using pseudocodes or you can use flowcharts.
1. Baking Bread
2. Logging into your laboratory's computer
3. Getting the average of three numbers
Introduction to Programming I 24