Chapter 1 Number and Code Systems
Chapter 1 Number and Code Systems
OUTCOMES: Upon completion of this topic, students should be able to: 1. illustrate the knowledge of digital number systems: decimal, binary, octal and hexadecimal 2. illustrate the knowledge of code systems: BCD 8421 and ASCII code
There are many types of number system is used in our life. The binary number system is the most important one in digital systems, but several others are also important. The decimal system is important because it is universally used to represent quantities outside a digital system. This means that there will be situations where decimal values must be converted to binary values before they are entered into the digital system. Likewise, there will be situations where the binary values at the outputs of a digital system must be converted to decimal values for presentation to the outside world. Two other number systems that also important are octal and hexadecimal system.
1.1
Number Systems There are 4 types of numbering system used in digital system. a. Decimal system (N10) 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 b. Binary system (N2) 0, 1 c. Octal system (N8) 0, 1, 2, 3, 4, 5, 6, 7 d. Hexadecimal system (N16) 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
Example:
Basic number
23410
Base number
Page 1-1
Binary Number System: System Digits: 0 and 1 Bit (short for binary digit): A single binary digit LSB (least significant bit): The rightmost bit MSB (most significant bit): The leftmost bit 1.2 Conversion Number Systems
1.2.1 Decimal to Binary Conversions The decimal number will divide by 2 until obtain minimum values. If the decimal number have the decimal point, so each the decimal point value must be multiply by 2. Example: Convert 12310 to it binary equivalent. Ans: 2 123 remainder of 1 2 61 remainder of 1 2 30 remainder of 0 2 15 remainder of 1 2 7 remainder of 1 2 3 remainder of 1 1 12310 = 1 1 1 1 0 1 12
MSB LSB
Page 1-2
Example: Convert 97.5410 to it binary equivalent. Ans: 2 2 2 2 2 2 97 48 24 12 6 3 1 1 0 0 0 0 1 0.54 0.08 0.16 0.32 x x x x 2= 2= 2= 2= 1 .08 0 .16 0 .32 0 .64
12310 = 1 1 0 0 0 0 1.12
1.2.2 Decimal to Octal Conversions The decimal number will divide by 8 until minimum values. If the decimal number have the decimal point, so each the decimal point value must be multiply by 8. Example: Convert 39610 to it octal equivalent. Ans: 8 396 remainder of 4 8 49 remainder of 1 6 39610 = 6 1 48
MSD LSD
Example: Convert 172.3110 to it octal equivalent. Ans: 8 172 8 21 2 4 5 0.31 0.48 0.84 0.72 x x x x 8= 8= 8= 8= 2 .48 3 .84 6 .72 5 . 76
172.3110 = 254.23658
Page 1-3
1.2.3 Decimal to Hexadecimal Conversions The decimal number will divide by 16 until minimum values. If the decimal number have the decimal point, so each the decimal point value must be multiply by 16. Example: Convert 698310 to it hexadecimal equivalent. Ans: 16 6983 remainder of 7 16 436 remainder of 4 16 27 remainder of 11 1 698310 = 1 B 4 716
MSD LSD
Example: Convert 5932.7610 to it binary equivalent. Ans: 16 5932 16 370 16 23 1 12 2 14 0.76 0.16 0.56 0.96 x x x x 16 = 12 .16 16 = 2 .56 16 = 8 .96 16 = 15 .36
5932.7610 = 1 E 2 C . C 2 8 F16
1.2.4 Binary to Decimal Conversions .25 .32 24 16 23 8 22 Binary System 21 20 4 2 1 2-1 0.5 2-2 0.25 2-3.. 0.125.
Example : Convert 1012 to it decimal equivalent. Ans: 1012 = (1 x 22) + (0 x 21) + (1 x 20) = (1 x 4) + (0 x 2) + (1 x 1) =4+0+1 = 510
Page 1-4
Example : Convert 1011.012 to it decimal equivalent. Ans: 1011.0112 = (1 x 23) + (0 x 22) + (1 x 21) + (1 x 20) + (0 x 2-1) + (1 x 2-2) + (1 x 2-3) = (1 x 8) + (0 x 4) + (1 x 2) + (1 x 1) + (0 x 0.5) + (1 x 0.25) + (1 x 0.125) = 8 + 0 + 2 + 1 + 0 + 0.25 + 0.125 = 11.37510
1.2.5 Octal to Decimal Conversions .....83 512 82 64 81 Octal System 80 8 1 8-1 0.125 8-2.. 0.015625.
Example : Convert 6258 to it decimal equivalent. Ans: 6258 = (6 x 82) + (2 x 81) + (5 x 80) = (6 x 64) + (2 x 8) + (5 x 1) = 384 + 16 + 5 = 40510
Example : Convert 31.78 to it decimal equivalent. Ans: 31.78 = (3 x 81) + (1 x 80) + (7 x 8-1) = (3 x 8) + (1 x 1) + (7 x 0.125) = 24 + 1 + 0.875 = 25.87510
1.2.6 Hexadecimal to Decimal Conversions .....163 4096 162 256 Hexadecimal System 161 160 16-1 16 1 0.0625 16-2.. 0.0039.
Example : Convert 1D216 to it decimal equivalent. Ans: 1D216 = (1 x 162) + (D x 161) + (2 x 160) = (1 x 256) + (13 x 16) + (2 x 1) = 256 + 208 + 2 = 46610
Page 1-5
Example : Convert A69.5C16 to it decimal equivalent. ANS: A69.5C16 = (A x 162) + (6 x 161) + (9 x 160) + (5 x 16-1) + (C x 16-2) = (10 x 256) + (6 x 16) + (9 x 1) + (5 x 0.0625) + (12 x 0.0039) = 2560 + 96 + 9 + 0.3125 + 0.0468 = 2665.359310
1.2.7 Binary to Octal Conversions Assign the each bit binary numbers into 3 bit from LSB to MSB. It is because octal value need 3 bit of binary value to complete the systems. Binary System 000 001 010 011 100 101 110 111 Octal System 0 1 2 3 4 5 6 7
0 1 1 0 1 0 1 0 0 3 2 4 110101002 = 3248
Page 1-6
1.2.8 Binary to Hexadecimal Conversions Assign the each bit binary numbers into 4 bit from LSB to MSB. It is because hexadecimal value need 4 bit of binary value to complete the systems. Binary System 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 Hexadecimal System 0 1 2 3 4 5 6 7 8 9 A B C D E F
0 0 1 0 2
1 1 0 1 1 0 1 0 D A
10110110102 = 2DA16
Page 1-7
1.2.9 Hexadecimal to Binary Conversions Example: Convert 195D16 to it binary equivalent. Ans: 1 9 5 D 0 0 0 1 1 0 0 1 0 1 0 1 1 1 0
MSB
1
LSB
195D16 = 11001010111012
8E2.A616 = 100011100010.10100112
1.2.10 Hexadecimal to Octal Conversions There are two methods to convert the hexadecimal to octal systems. The methods are: This method is easier and it can reduce calculation careless. 1. Hexadecimal Binary Octal 2. Hexadecimal Decimal Octal Example: Convert 62E316 to it octal equivalent. ANS: Method 1: Hexadecimal Binary Octal Hexa 6 2 E 3 Binary 0 0 0 1 1 0 0 0 1 0 1 1 1 0 0 0 1 1 Octal 0 6 1 3 4 3 62E316 = 613438
Method 2: Hexadecimal Decimal Octal Hexadecimal Decimal : 62E316 = (6 x 163) + (2 x 162) + (14 x 161) + (3 x 160) = (6 x 4096) + (2 x 256) + (14 x 16) + (3 x 1) = 24576 + 512 + 224 + 3 = 2531510
Prepared by: WAN HAMIDAH BINTI WAN ABAS [PKK\JKE]_version01_Dis2012 Page 1-8
Decimal Octal :
3 4 3 1
2531510 = 613438
Example : Convert 2C.1916 to it octal equivalent. Ans: Method 1: Hexadecimal Binary Octal Hexa 2 C 1 9 Binary 0 0 0 1 0 1 1 0 0 0 0 0 1 1 0 0 1 0 Octal 0 5 4 0 6 2 2C.1916 = 54.0628
Method 2: Hexadecimal Decimal Octal Hexadecimal Decimal : 2C.1916 = (2 x 161) + (12 x 160) + (1 x 16-1) + (9 x 16-2) = (2 x 16) + (12 x 1) + (1 x 0.0625) + (9 x 0.00391) = 32 + 12 + 0.0625 + 0.03519 = 44.0976910 Decimal Octal : 0.09769 x 8 = 0 .78152 0.78152 x 8 = 6 .25216 0.25216 x 8 = 2 .01728
44 5
44.0976910 = 54.0628
2C.1916 = 54.0628
Page 1-9
1.2.11 Octal to Hexadecimal Conversions There are two methods to convert the hexadecimal to octal systems. The methods are: This method is easier and it can reduce calculation careless. 1. Octal Binary Hexadecimal 2. Octal Decimal Hexadecimal
Example : Convert 42578 to it hexadecimal equivalent. Ans: Method 1: Octal Binary Hexadecimal Octal 4 2 5 7 Binary 1 0 0 0 1 0 1 0 1 1 1 1 Hexa 8 A F 42578 = 8AF16
Method 2: Octal Decimal Hexadecimal Octal Decimal : 42578 = (4 x 83) + (2 x 82) + (5 x 81) + (7 x 80) = (4 x 512) + (2 x 64) + (5 x 8) + (7 x 1) = 2048 + 128 + 40 + 7 = 222310 Decimal Hexadecimal: 16 16 2223 138 8 F A
Example : Convert 741.258 to it hexadecimal equivalent. Ans: Method 1: Octal Binary Hexadecimal Octal 7 4 1 2 5 Binary 0 0 0 1 1 1 1 0 0 0 0 1 0 1 0 1 0 1 0 0 Hexa 1 E 1 5 4 741.258 = 1E1.5416
Page 1-10
Method 2: Octal Decimal Hexadecimal Octal Decimal : 741.258 = (7 x 82) + (4 x 81) + (1 x 80) + (2 x 8-1) + (5 x 8-2) = (7 x 64) + (4 x 8) + (1 x 1) + (2 x 0.125) + (5 x 0.015625) = 448 + 32 + 1 + 0.25 + 0.078125 = 481.3281210 Decimal Hexadecimal: 16 16 481 30 1 1 E 0.32812 x 16 = 5 .24992 0.24992 x 16 = 3 .99872 0.99872 x 16 = F .97952
REMEMBER!!!
CONVERSION NUMBER SYSTEMS Number system positions N2 N1 N0
.N5
N4
N3
N-1
N-2
N-3..
Multiplication
Decimal
Division
Decimal
Multiply
Page 1-11
1.3
Note: The rules of binary addition (without carries) are the same as the truths of the XOR gate. Example : 1. 10102 + 1112 = 100012
1 1
= = =
1 1 1 1 02 + 1 0 0 1 12 1 1 0 0 0 12
= = =
1.3.1.2
Subtraction
Rules of Binary Subtraction: 0-0=0 0 - 1 = 1 and borrow 1 from the next more significant 1-0=1 1-1=0
1 0 1 0 12 1 1 02 1 1 1 12
= = =
1.3.1.3
Multiplication
Rules of Binary Multiplication: 00=0 01=0 10=0 1 1 = 1 and no carry or borrow bits
Note: The rules of binary multiplication are the same as the truths of the AND gate. Example : 1. 1012 112 = 11112 101 11 101 + 101 1111 = = 510 310 1510
1.3.1.4
Division Binary division is the repeated process of subtraction, just as in decimal division. Example: 1. 1010102 1102 = 1112 111 110 101010 - 110 1001 110 110 110
...
Page 1-13
2. 100001112 1012 = 110112 11011 101 10000111 - 101 110 - 101 11 0 111 101 101 101
...
1.3.2 Octal Arithmetic 1.3.2.1 Addition Example: 1. 6538 + 1228 =7758 6 5 38 + 1 2 28 7 7 58 2. 7168 + 1548 =7758
1 1
6 + 4 = 10 8 = 2 7+1= 8 8=0
Page 1-14
8 2 5 8
3 6 4 58 - 7 5 18 2 6 7 48 1.3.3 Hexadecimal Arithmetic 1.3.3.1 Addition Example: 1. 952116 + 1A616 =96C716 9 5 2 116 + 1 A 616 9 6 C 716 2. 3F216 + 6E16 =46016
1 1
4 + 8 = 12 5 = 7 5 + 8 = 13 7 = 6
3 F 216 + 6 E16 4 6 016 1.3.3.2 Subtraction Example: 1. 2B9916 39816 =280116 2 B 9 916 3 9 816 2 8 0 116
2 + 14 = 16 16 = 0 15 + 1 + 6 = 22 16 = 6
16 D E 16
16 + 3 = 19 15 = 4 16 + 14 = 30 - 15 = F
Page 1-15
1.4
Sign Numbers In digital systems, the binary numbers are represented by a set of binary such as a microprocessor register 8 bits. The most significant bit (MSB) is the sign bit. If this bit is 0, then the number is (+) positive. However, if the sign bit is 1, then the number is (-) negative. The other 7 bits in this 8-bit register represent the magnitude of the number.
Sign bit 0 = (+) 1 = () MSB Magnitude LSB
0
Sign bit (+)
1
True binary
= +4510
A7
A6
A5
A4
A3
A2
A1
A0
1
Sign bit (-)
= -4510
2s complement
1.5
2s Complement Numbers The 2s complement method of representing numbers is widely used in microprocessor based equipment. Until now, we have assumed that all numbers are positive. However, microprocessors must process both positive and negative numbers. By using 2s complement representation, the sign as well as the magnitude of a number can be determined. 1s Complement The 1s complement of a binary number is obtained by changing each 0 to a 1 and each 1 to a 0. In other words, change each bit in the number to its complement. Example: Original binary number 1s complement
0 1
0 1
1 0
0 1
1 0
1 0
0 1
1 0
Page 1-16
2s Complement The 2s complement of a binary number is formed by taking the 1s complement of the number and adding 1 to the least-significant-bit position. Example: Original binary number 1s complement Add 1 to form 2s complement 2s complement of original number 0 1 + 1 1 0 1 0 0 1 0 1 1 0 0 1 1 0 1 0 0 1 1 0 1 1
Example: Represent the signed decimal numbers below as a signed binary number in 2s complement system. Use a total of five bits including the sign bit. a) + 13 b) 9 Ans: a) + 13 = 01101 sign bit b) + 9 change to 9 by using 2s complement +9 1s complement Add 1 2s complement 9 = 10111 01001 10110 + 1 10111
1.5.1 Arithmetic of 2s Complement Case I: Two Positive Numbers The addition of two positive numbers is straight forward. Example: + 9 + 4 = + 13 +9 0 1001 + 4 + 0 0100 0 1101 sign bits
Prepared by: WAN HAMIDAH BINTI WAN ABAS [PKK\JKE]_version01_Dis2012 Page 1-17
Case II: Positive Number and Smaller Negative Number Consider the addition of +9 and 4. Remember that the 4 will be in its 2s complement form. Thus, +4 (00100) must be converted to 4 (11100). Example: +94=+5 sign bits +9 0 1001 - 4 + 1 1100 1 0 0101 This carry is disregarded; the result is 00101 (sum = +5)
Case III: Positive Number and Larger Negative Number Consider the addition of 9 and + 4. Example: -9+4=-5 -9 1 0111 + 4 + 0 0100 1 1011 negative sign bits
(sum = - 5)
Case IV: Two Negative Numbers Consider the addition of 9 and 4. Example: - 9 4 = - 13 sign bits -9 1 0111 - 4 + 1 1100 1 1 0011 This carry is disregarded; the result is 10011 (sum = - 13) Case V: Equal and Opposite Numbers Example: -9+9=+0 sign bits -9 1 0111 + 9 + 0 1001 1 0 0000 This carry is disregarded; the result is 00000 (sum = + 0)
Prepared by: WAN HAMIDAH BINTI WAN ABAS [PKK\JKE]_version01_Dis2012 Page 1-18
1.5.2 Arithmetic Overflow Example: incorrect sign bits +9 0 1001 + 8 + 0 1000 1 0001 The answer has a negative sign bit, which is obviously incorrect since we are adding two positive numbers. The answer should be +17, but the magnitude 17 requires more than four bits and therefore overflows into the sign-bit position. This overflow condition can occur only when two positive or two negative numbers are being added, and it always produces an incorrect result. Overflow can be detected by checking to see that the sign bit of the result is the same as the sign bits of the numbers being added.
Example 1: Convert the following decimal value to BCD code. a) 593110 b) 28.9410 Ans: a) 593110 = 101100100110001BCD Decimal BCD code 5 9 3 1
0 1 0 1 1 0 0 1 0 0 1 1 0 0 0 1
Page 1-19
0 0 1 0 1 0 0 0 1 0 0 1 0 1 0 0
Example 2: Convert the following BCD code to it decimal value equivalent. a) 10101010101000101BCD b) 101001100.1001011BCD Ans: a) 10101010101000101BCD = 1554510 Decimal 0 0 0 1 0 1 0 1 0 1 0 1 0 1 0 0 0 1 0 1 BCD code 1 5 5 4 5 b) 101000011.1001011BCD= 143.9610 Decimal 0 0 0 1 0 1 0 0 0 0 1 1 1 0 0 1 0 1 1 0 BCD code 1 4 3 9 6
1.6.1.1 Arithmetic of BCD code BCD addition follows the same rules as binary addition. However, if the addition produces a carry and/or creates an invalid BCD number, an adjustment is required to correct the sum. The correction method is to add 6 to the sum in any digit position that has caused an error. Case I: Sum Equals 9 or Less Example: 1. +
5 4 9
2.
45 + 33 78
Page 1-20
3.
25 + 12 37
0110BCD 0111BCD 1101BCD invalid code group for BCD 0110BCD add 6 for correction 0011BCD 3
47 + 35 82
+ +
0111BCD 0101BCD 1100BCD invalid sum in first digit 0110BCD add 6 for correction 0010BCD correct BCD sum 2
3.
59 + 38 97
+ +
11 0101 1001BCD 0011 1000BCD 1001 0001BCD invalid sum in first digit 0110BCD add 6 for correction 1001 0111BCD correct BCD sum 9 7
BCD subtraction follows the same rules as binary subtraction. However, if the subtraction causes a borrow and/or creates an invalid BCD number, an adjustment is required to correct the answer. The correction method is to subtract 6 from the difference in any digit position that has caused an error.
Page 1-21
1.6.2 ASCII Code ASCII code is used to transmit the digital data through telephone line. Table 1 shown the sequential of ASCII code table that have alphabets, numbers and some symbols. ASCII code has 7 bit code. For example, if the data was transmitted is 100 0001, that means the data is A. So, 100 0001 = A.
Page 1-22
Example 2: The following is a message encoded in ASCII code. What is the message? 1001000 10001010 1001100 1010000 Ans: 4 8 1 0 0 1 0 0 0 H 4 5 1 0 0 0 1 0 1 E 4 C 1 0 0 1 1 0 0 L 5 0 1 0 1 0 0 0 0 P
Page 1-23