The efficient use of a microprocessor or microcontroller requires a working knowledge of binary, decimal, and hexadecimal numbering systems. This section provides a background for those who are unfamiliar with these numbering systems and who do not know how to convert from one number system to another one.
Number systems are classified according to their bases. The numbering system used in everyday life is base 10 or the decimal number system. The most commonly used numbering system in microprocessor and microcontroller applications is base 16, or hexadecimal. In addition, base 2 (binary) or base 8 (or octal) number systems are also used.
A.2. Decimal number system
As you all know the numbers in this system are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. We can use the subscript 10 to indicate that a number is in decimal format. For example, we can show decimal number 235 as 23510.
In general, a decimal number is represented as follows:
an×10n+an−1×10n−1+an−2×10n−2+⋯+a0×100
For example, decimal number 82510 can be shown as follows:
82510=8×102+2×101+5×100
Similarly, decimal number 2610 can be shown as:
2610=2×101+6×100
or,
335910=3×103+3×102+5×101+9×100
A.3. Binary number system
In binary number system, there are two numbers: 0 and 1. We can use the subscript 2 to indicate that a number is in binary format. For example, we can show binary number 1011 as 10112.
In general, a decimal number is represented as follows:
an×2n+an−1×2n−1+an−2×2n−2+⋯+a0×20
For example, binary number 11102 can be given as follows:
11102=1×23+1×22+1×21+0×20
Similarly, binary number 100011102 can be shown as:
100011102=1×27+0×26+0×25+0×24+1×23+1×22+1×21+0×20
A.4. Octal number system
In octal number system, the valid numbers are 0, 1, 2, 3, 4, 5, 6, 7. We can use the subscript 8 to indicate that a number is in octal format. For example, we can show octal number 23 as 238.
In general, an octal number is represented as:
an×8n+an−1×8n−1+an−2×8n−2+⋯+a0×80
For example, octal number 2378 can be given as:
2378=2×82+3×81+7×80
Similarly, octal number 17778 can be given as:
17778=1×83+7×82+7×81+7×80
A.5. Hexadecimal number system
In hexadecimal number system, the valid numbers are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F. We can use the subscript 16 of H to indicate that a number is in hexadecimal format. For example, we can show hexadecimal number 1F as 1F16 or as 1FH.
In general, a hexadecimal number is represented as:
an×16n+an−1×16n−1+an−2×16n−2+⋯+a0×160
For example, hexadecimal number 2AC16 can be given as:
2AC16=2×162+10×161+12×160
Similarly, hexadecimal number 3FFE16 can be represented as:
3FFE16=3×163+15×162+15×161+14×160
A.6. Converting binary numbers ınto decimal
To convert a binary number into decimal, write the number as the sum of the powers of 2.
Table A.1 presents the decimal equivalent of numbers from 0 to 31.
Table A.1
Decimal equivalent of binary numbers.
Binary
Decimal
Binary
Decimal
00000000
0
00010000
16
00000001
1
00010001
17
00000010
2
00010010
18
00000011
3
00010011
19
00000100
4
00010100
20
00000101
5
00010101
21
00000110
6
00010110
22
00000111
7
00010111
23
00001000
8
00011000
24
00001001
9
00011001
25
00001010
10
00011010
26
00001011
11
00011011
27
00001100
12
00011100
28
00001101
13
00011101
29
00001110
14
00011110
30
00001111
15
00011111
31
A.7. Converting decimal numbers ınto binary
To convert a decimal number into binary, divide the number repeatedly by 2 and take the remainders. The first remainder is the least significant digit (LSD), and the last remainder is the most significant digit (MSD).
Example A.3
Convert decimal number 2810 into binary.
Solution A.3
Divide the number into 2 repeatedly and take the remainders:
28/2
→
14
Remainder 0
(LSD)
14/2
→
7
Remainder 0
7/2
→
3
Remainder 1
3/2
→
1
Remainder 1
½
→
0
Remainder 1
(MSD)
The required binary number is 111002.
Example A.4
Convert decimal number 6510 into binary.
Solution A.4
Divide the number into 2 repeatedly and take the remainders:
65/2
→
32
Remainder 1
(LSD)
32/2
→
16
Remainder 0
16/2
→
8
Remainder 0
8/2
→
4
Remainder 0
4/2
→
2
Remainder 0
2/2
→
1
Remainder 0
½
→
0
Remainder 1
(MSD)
The required binary number is 10000012.
Example A.5
Convert decimal number 12210 into binary.
Solution A.5
Divide the number into 2 repeatedly and take the remainders:
122/2
→
61
Remainder 0
(LSD)
61/2
→
30
Remainder 1
30/2
→
15
Remainder 0
15/2
→
7
Remainder 1
7/2
→
3
Remainder 1
3/2
→
1
Remainder 1
½
→
0
Remainder 1
(MSD)
The required binary number is 11110102.
A.8. Converting binary numbers ınto hexadecimal
To convert a binary number into hexadecimal, arrange the number in groups of 4 and find the hexadecimal equivalent of each group. If the number cannot be divided exactly into groups of 4, insert zeroes to the left-hand side of the number.
Example A.6
Convert binary number 100111112 into hexadecimal.
Solution A.6
First, divide the number into groups of 4 and then find the hexadecimal equivalent of each group:
10011111=100111119F
The required hexadecimal number is 9F16.
Example A.7
Convert binary number 11101111000011102 into hexadecimal.
Solution A.7
First, divide the number into groups of 4 and then find the equivalent of each group:
1110111100001110=1110111100001110EF0E
The required hexadecimal number is EF0E16
Example A.8
Convert binary number 1111102 into hexadecimal.
Solution A.8
Since the number cannot be divided exactly into groups of 4, we have to insert zeroes to the left-hand side of the number:
111110=001111103E
The required hexadecimal number is 3E16.
Table A.2 presents the hexadecimal equivalent of numbers 0 to 31.
Table A.2
Hexadecimal equivalent of decimal numbers.
Decimal
Hexadecimal
Decimal
Hexadecimal
0
0
16
10
1
1
17
11
2
2
18
12
3
3
19
13
4
4
20
14
5
5
21
15
6
6
22
16
7
7
23
17
8
8
24
18
9
9
25
19
10
A
26
1A
11
B
27
1B
12
C
28
1C
13
D
29
1D
14
E
30
1E
15
F
31
1F
A.9. Converting hexadecimal numbers ınto binary
To convert a hexadecimal number into binary, write the 4-bit binary equivalent of each hexadecimal digit.
Example A.9
Convert hexadecimal number A916 into binary.
Solution A.9
Writing the binary equivalent of each hexadecimal digit:
A=101029=10012
The required binary number is 101010012.
Example A.10
Convert hexadecimal number FE3C16 into binary.
Solution A.10
Writing the binary equivalent of each hexadecimal digit:
F=11112E=111023=00112C=11002
The required binary number is 11111110001111002.
A.10. Converting hexadecimal numbers ınto decimal
To convert a hexadecimal number into decimal, we have to calculate the sum of the powers of 16 of the number.
Example A.11
Convert hexadecimal number 2AC16 into decimal.
Solution A.11
Calculating the sum of the powers of 16 of the number:
2AC16=2×162+10×161+12×160=512+160+12=684
The required decimal number is 68410.
Example A.12
Convert hexadecimal number EE16 into decimal.
Solution A.12
Calculating the sum of the powers of 16 of the number:
EE16=14×161+14×160=224+14=238
The required decimal number is 23810.
A.11. Converting decimal numbers ınto hexadecimal
To convert a decimal number into hexadecimal, divide the number repeatedly into 16 and take the remainders. The first remainder is the LSD, and the last remainder is the MSD.
Example A.13
Convert decimal number 23810 into hexadecimal.
Solution A.13
Dividing the number repeatedly into 16:
238/16
→
14
Remainder 14 (E)
(LSD)
14/16
→
0
Remainder 14 (E)
(MSD)
The required hexadecimal number is EE16.
Example A.14
Convert decimal number 68410 into hexadecimal.
Solution A.14
Dividing the number repeatedly into 16:
684/16
→
42
Remainder 12 (C)
(LSD)
42/16
→
2
Remainder 10 (A)
2/16
→
0
Remainder 2
(MSD)
The required hexadecimal number is 2AC16.
A.12. Converting octal numbers ınto decimal
To convert an octal number into decimal, calculate the sum of the powers of 8 of the number.
Example A.15
Convert octal number 158 into decimal.
Solution A.15
Calculating the sum of the powers of 8 of the number:
158=1×81+5×80=8+5=13
The required decimal number is 1310.
Example A.16
Convert octal number 2378 into decimal.
Solution A.16
Calculating the sum of the powers of 8 of the number:
2378=2×82+3×81+7×80=128+24+7=159
The required decimal number is 15910.
A.13. Converting decimal numbers ınto octal
To convert a decimal number into octal, divide the number repeatedly into 8 and take the remainders. The first remainder is the LSD, and the last remainder is the MSD.
Example A.17
Convert decimal number 15910 into octal.
Solution A.17
Dividing the number repeatedly into 8:
159/8
→
19
Remainder 7
(LSD)
19/8
→
2
Remainder 3
2/8
→
0
Remainder 2
(MSD)
The required octal number is 2378.
Example A.18
Convert decimal number 46010 into octal.
Solution A.18
Dividing the number repeatedly into 8:
460/8
→
57
Remainder 4
(LSD)
57/8
→
7
Remainder 1
7/8
→
0
Remainder 7
(MSD)
The required octal number is 7148.
Table A.3 presents the octal equivalent of decimal numbers 0 to 31.
Table A.3
Octal equivalent of decimal numbers.
Decimal
Octal
Decimal
Octal
0
0
16
20
1
1
17
21
2
2
18
22
3
3
19
23
4
4
20
24
5
5
21
25
6
6
22
26
7
7
23
27
8
10
24
30
9
11
25
31
10
12
26
32
11
13
27
33
12
14
28
34
13
15
29
35
14
16
30
36
15
17
31
37
A.14. Converting octal numbers ınto binary
To convert an octal number into binary, write the 3-bit binary equivalent of each octal digit.
Example A.19
Convert octal number 1778 into binary.
Solution A.19
Write the binary equivalent of each octal digit:
1=00127=11127=1112
The required binary number is 0011111112.
Solution A.20
Convert octal number 758 into binary.
Solution A.20
Write the binary equivalent of each octal digit:
7=11125=1012
The required binary number is 1111012.
A.15. Converting binary numbers ınto octal
To convert a binary number into octal, arrange the number in groups of 3 and write the octal equivalent of each digit.
Example A.21
Convert binary number 1101110012 into octal.
Solution A.21
Arranging in groups of 3:
110111001=110111001671
The required octal number is 6718.
A.16. Negative numbers
The most significant bit of a binary number is usually used as the sign bit. By convention, for positive numbers this bit is 0, and for negative numbers this bit is 1. Fig. A.1 shows the 4-bit positive and negative numbers. The largest positive and negative numbers are +7 and −8, respectively.
To convert a positive number into negative, take the complement of the number and add 1. This process is also called the 2’s complement of the number.
Example A.22
Write decimal number −6 as a 4-bit number.
Solution A.22
First, write the number as a positive number, then find the complement and add 1:
0110
+6
1001
compliment
1
add 1
-------
1010
which is −6
Example A.23
Write decimal number −25 as a 8-bit number.
Solution A.23
First, write the number as a positive number, then find the complement and add 1:
00011001
+25
11100110
compliment
1
add 1
-------------
11100111
which is −25
A.17. Adding binary numbers
The addition of binary numbers is similar to the addition of decimal numbers. Numbers in each column are added together with a possible carry from a previous column. The primitive addition operations are:
0 + 0 = 0
0 + 1 = 1
1 + 0 = 1
1 + 1 = 10
generate a carry bit
1 + 1 + 1 = 11
generate a carry bit
Some examples are given below.
Example A.24
Find the sum of binary numbers 011 and 110.
Solution A.24
We can add these numbers as in the addition of decimal numbers:
011
First column
1 + 0 = 1
+ 110
Second column
1 + 1 = 0 and a carry bit
--------
Third column
1 + 1 = 10
1001
Example A.25
Find the sum of binary numbers 01000011 and 00100010.
Solution A.25
We can add these numbers as in the addition of decimal numbers:
01000011
First column
1 + 0 = 1
+ 00100010
Second column
1 + 1 = 10
----------------
Third column
0 + carry = 1
01100101
Fourth column:
0 + 0 = 0
Fifth column:
0 + 0 = 0
Sixth column:
0 + 1 = 1
Seventh column:
1 + 0 = 1
Eighth column:
0 + 0 = 0
A.18. Subtracting binary numbers
To subtract two numbers, convert the number to be subtracted into negative and then add the two numbers.
Example A.26
Subtract binary number 0010 from 0110.
Solution A.26
First, let’s convert the number to be subtracted into negative:
0010
number to be subtracted
1101
compliment
1
add 1
-------
1110
Now, add the two numbers:
0110
+ 1110
----------
0100
Since we are using 4-bits only, we cannot show the carry bit.
A.19. Multiplication of binary numbers
Multiplication of two binary numbers is same as the multiplication of decimal numbers. The four possibilities are:
0 × 0 = 0
0 × 1 = 0
1 × 0 = 0
1 × 1 = 1
Some examples are given below.
Example A.27
Multiply the two binary numbers 0110 and 0010.
Solution A.27
Multiplying the numbers:
0110
0010
-------
0000
0110
0000
0000
-------
001100 or 1100
In this example, 4-bits are needed to show the final result.
Example A.28
Multiply binary numbers 1001 and 1010.
Solution A.28
Multiplying the numbers:
1001
1010
-------
0000
1001
0000
1001
------------
1011010
In this example, 7-bits are required to show the final result.
A.20. Division of binary numbers
The division of binary numbers is similar to the division of decimal numbers. An example is given below.
Example A.29
Divide binary number 1110 into binary number 10.
Solution A.29
Dividing the numbers:
111
----------
10
---
11
10
---
10
10
---
00
giving the result 1112.
A.21. Floating point numbers
Floating point numbers are used to represent non-integer fractional numbers. For example: 3.256, 2.1, 0.0036, and so forth. Floating point numbers are used in most engineering and technical calculations. The most commonly used floating point standard is the IEEE standard. According to this standard, floating point numbers are represented with 32-bit (single precision) or 64-bit (double precision).
In this section, we are looking at the format of 32-bit floating point numbers only and see how mathematical operations can be performed with such numbers.
According to the IEEE standard, 32-bit floating point numbers are represented as:
31
30
23
22
0
X
XXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXX
↑
↑
↑
sign
exponent
mantissa
The most significant bit indicates sign of the number, where 0 indicates positive and 1 indicates that the number is negative.
The 8-bit exponent shows the power of the number. To make the calculations easy, the sign of the exponent is not shown, but instead excess 128 numbering system is used. Thus, to find the real exponent, we have to subtract 127 from the given exponent. For example, if the mantissa is “10000000”, the real value of the mantissa is 128 − 127 = 1.
The mantissa is 23-bits wide and represents the increasing negative powers of 2. For example, if we assume that the mantissa is: “1110000000000000000000”, the value of this mantissa is calculated as: 2−1 + 2−2 + 2−3 = 7/8.
The decimal equivalent of a floating point number can be calculated using the formula:
Number=(−1)s2e−1271.f
where
s = 0 for positive numbers, 1 for negative numbers
e = exponent (between 0 and 255)
f = mantissa
As shown in the above formula, there is a hidden “1” in-front of the mantissa. That is, mantissa is shown as “1.f.”
The largest and the smallest numbers in 32-bit floating point format are:
The largest number
01111111011111111111111111111111
This number is: (2 − 2−23) 2127 or decimal 3.403 × 1038. The numbers keep their precision up to 6 digits after the decimal point.
The smallest number
00000000100000000000000000000000
This number is: 2−126 or decimal 1.175 × 10−38.
A.22. Converting a floating point number ınto decimal
To convert a given floating point number into decimal, we have to find the mantissa and the exponent of the number and then convert into decimal as shown above.
Some examples are given here.
Example A.30
Find the decimal equivalent of the floating point number given below:
01000000110000000000000000000000
Solution A.30
Here,
sign = positive
exponent = 129 − 127 = 2
mantissa = 2−1 = 0.5
The decimal equivalent of these number is +1.5 × 22 = +6.0
Example A.31
Find the decimal equivalent of the floating point number given below:
01000001011000000000000000000
Solution A.31
In this example,
sign = positive
exponent = 130 − 127 = 3
mantissa = 2−1 + 2−2 = 0.75
The decimal equivalent of the number is +1.75 × 23 = 14.0
A.22.1. Normalizing the floating point numbers
Floating point numbers are usually shown in normalized form. A normalized number has only one digit before the decimal point (A hidden number 1 is assumed before the decimal point).
To normalize a given floating point number, we have to move the decimal point repetitively one digit to the left and then increase the exponent after each move.
Some examples are given below.
Example A.32
Normalize the floating point number 123.56
Solution A.32
If we write the number with a single digit before the decimal point, we get:
1.2356×102
Example A.33
Normalize the binary number 1011.12
Solution A.33
If we write the number with a single digit before the decimal point, we get:
1.1113
A.22.2. Converting a decimal number into floating point
To convert a given decimal number into floating point, we have to carry out the following steps:
• Write the number in binary
• Normalize the number
• Find the mantissa and the exponent
• Write the number as a floating point number
Some examples are given below:
Example A.34
Convert decimal number 2.2510 into floating point.
Solution A.34
Writing the number in binary:
2.2510=10.012
Normalizing the number,
10.012=1.0012×21
Here, s = 0, e − 127 = 1 or e = 128, and f = 00100000000000000000000
(Remember that a number 1 is assumed on the left-hand side, even though it is not shown in the calculation). We can now write the required floating point number as:
s
e
f
0
10000000
(1)001 0000 0000 0000 0000 0000
or, the required 32-bit floating point number is:
01000000000100000000000000000000
Example A.35
Convert the decimal number 134.062510 into floating point.
Solution A.35
Writing the number in binary:
134.062510=10000110.0001
Normalizing the number,
10000110.0001=1.00001100001×27
Here, s = 0, e − 127 = 7 or e = 134, and f = 00001100001000000000000
We can now write the required floating point number as:
s
e
f
0
10000110
(1)00001100001000000000000
or, the required 32-bit floating point number is:
01000011000001100001000000000000
A.22.3. Multiplication and division of floating point numbers
The multiplication and division of floating point numbers is rather easy and the steps are given below:
• Add (or subtract) the exponents of the numbers
• Multiply (or divide) the mantissa of the numbers
• Correct the exponent
• Normalize the number
• The sign of the result is the EXOR of the signs of the two numbers
Since the exponent is processed twice in the calculations, we have to subtract 127 from the exponent.
An example is given below to show the multiplication of two floating point numbers.
Example A.36
Show the decimal numbers 0.510 and 0.7510 in floating point and then calculate the multiplication of these numbers.
Solution A.36
We can convert the numbers into floating point as:
0.510=1.0000×2−1
Here, s = 0, e − 127 = −1 or e = 126, and f = 0000
or,
0.510=001110110(1)00000000000000000000000
Similarly,
0.7510=1.1000×2−1
Here, s = 0, e = 126, and f = 1000
or,
0.7510=001110110(1)10000000000000000000000
Multiplying the mantissas, we get “(1)100 0000 0000 0000 0000 0000.” The sum of the exponents is 126 + 126 = 252. Subtracting 127 from the mantissa, we obtain, 252 − 127 = 125. The EXOR of the signs of the numbers is 0. Thus, the result can be shown in floating point as:
001111101(1)10000000000000000000000
The above number is equivalent to decimal 0.375 (0.5 × 0.75 = 0.375), which is the correct result.
A.22.4. Addition and subtraction of floating point numbers
The exponents of floating point numbers must be the same before they can be added or subtracted. The steps to add or subtract floating point numbers are:
• Shift the smaller number to the right until the exponents of both numbers are the same. Increment the exponent of the smaller number after each shift.
• Add (or subtract) the mantissa of each number as an integer calculation, without considering the decimal points.
• Normalize the obtained result.
An example is given below.
Example A.37
Show decimal numbers 0.510 and 0.7510 in floating point and then calculate the sum of these numbers.
Solution A.37
As shown in Example A.36, we can convert the numbers into floating point as:
0.510=001110110(1)00000000000000000000000
Similarly,
0.7510=001110110(1)10000000000000000000000
Since the exponents of both numbers are the same, there is no need to shift the smaller number. If we add the mantissa of the numbers without considering the decimal points, we get:
(1)000 0000 0000 0000 0000 0000
(1)100 0000 0000 0000 0000 0000
____________________________+
(10)100 0000 0000 0000 0000 0000
To normalize the number, we can shift it right by one digit and then increment its exponent. The resulting number is:
001111111(1)01000000000000000000000
The above floating point number is equal to decimal number 1.25, which is the sum of decimal numbers 0.5 and 0.75.
To convert floating point numbers into decimal, and decimal numbers into floating point, the freely available program given in the following website can be used:
BCD (Binary Coded Decimal) numbers are usually used in display systems such as LCDs and 7-segment displays to show numeric values. In BCD, each digit is a 4-bit number from 0 to 9. As an example, Table A.4 presents the BCD numbers between 0 and 20.
Table A.4
BCD numbers between 0 and 20.
Decimal
BCD
Binary
0
0000
0000
1
0001
0001
2
0010
0010
3
0011
0011
4
0100
0100
5
0101
0101
6
0110
0110
7
0111
0111
8
1000
1000
9
1001
1001
10
0001 0000
1010
11
0001 0001
1011
12
0001 0010
1100
13
0001 0011
1101
14
0001 0100
1110
15
0001 0101
1111
16
0001 0110
1 0000
17
0001 0111
1 0001
18
0001 1000
1 0010
19
0001 1001
1 0011
20
0010 0000
1 0100
Example A.38
Write the decimal number 295 as a BCD number
Solution A.38
Writing the 4-bit binary equivalent of each digit,
2=001029=100125=01012
The required BCD number is: 0010 1001 01012
Example A.39
Write the decimal equivalent of BCD number 1001 1001 0110 00012
Solution A.39
Writing the decimal equivalent of each group of 4-bit, we get the decimal number,