01 Number Systems
Click here to load reader
-
Upload
season0320 -
Category
Documents
-
view
261 -
download
0
Transcript of 01 Number Systems
![Page 1: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/1.jpg)
1
EE2000 Logic Circuit Design
Number Systems & Arithmetic
![Page 2: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/2.jpg)
2
OutlineNumber systems
Conversion between different radix systemsBinary arithmetic
Complement of binary numbersSigned number representations
Codes in Digital WorldBCDASCIIGray codes
Parity and error correction
![Page 3: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/3.jpg)
3
Number Systems
Decimal number system employed in everyday arithmeticRepresent numbers by strings of digits called positional notation
Digits – the Latin word for fingersan-1 an-2 … a2 a1 a0 (0 ≤ ai < 10)Each digit is associated to a value depends on its position in the string
![Page 4: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/4.jpg)
4
Polynomial Form of IntegerThe no. can be expressed in power seriesN = an-1rn-1 + an-2rn-2 + … + a2r2 + a1r + a0
N: the decimal value of the integern: number of digitsr: radix (base)ai: coefficients (digit), 0 ≤ ai < r
e.g. (7672)104 digits, so n = 4decimal number, i.e. r = 10ai: 0 ≤ ai < 10, i.e. one of the ten digits (0, 1, 2, …, 9)N = 7 x 103 + 6 x 102 + 7 x 10 + 2
These two 7 symbols have different meaning
![Page 5: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/5.jpg)
5
Floating Point Numberan-1 an-2 … a2 a1 a0 . a-1 a-2 … a-(m-1) a-m (0 ≤ ai < 10)
“.” is the radix pointThe left most digit an-1 is the most significant digit (MSD)The right most digit a-m is the least significant digit (LSD)
General polynomial formN = an-1rn-1 + … + a1r1 + a0r0 + a-1r -1 + … + a-mr -m
Number of digits = n + me.g. (767.2)10
n = 3, m = 1N = 7 x 102 + 6 x 101 + 7 + 2 x 10-1
![Page 6: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/6.jpg)
6
Alternative Conversion Method
(…((an-1r + an-2)r + an-3)r + … + a1)r + a0
+(a-1+ (a-2 + (… + (a-m+1+ a-mr -1)r -1)…r -1)r -1
e.g. (767.295)10
= ((7 x 10 + 6)10 + 7)+ (2 + (9 + 5 x 10-1)10-1)10-1
![Page 7: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/7.jpg)
7
Other Number Systems
Decimal (base 10) are for human
Three number systems are commonly used in computer work
Binary (base 2)Octal (base 8)Hexadecimal (base 16)
![Page 8: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/8.jpg)
8
Binary Number SystemA base 2 system with two digits: 0 and 1Expressed with a string of 1s and 0sThe digits are called bits(binary digits).The left most bit called most significant bit (MSB)The right most bit called least significant bit (LSB)
Decimal Binary01234567
01
1011
100101110111
![Page 9: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/9.jpg)
9
Radix Conversion
Convert from binary to decimale.g. convert (1011.11)2 to decimal
= 1 x 23 + 0 x 22 + 1 x 2 + 1 + 1 x 2-1 + 1 x 2-2
= 8 + 0 + 2 + 1 + 0.5 + 0.25= (11.75)10
i.e. the summation of the power series only
Powers of 2n 0 1 2 3 4 … 202n 1 2 4 8 16 … 1,048,576
![Page 10: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/10.jpg)
10
From Decimal to Binary
Convert from decimal to binarye.g. convert (746)10 to binary
= 7 x 102 + 4 x 101 + 6 x 100
= 111 x 101010 + 100 x 10101 + 110 x 10100
Involve binary multiplication!Time consuming and not easy to compute
![Page 11: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/11.jpg)
11
Two MethodsMethod 1: Subtraction
Subtract from the largest power of 2 that less than or equal to that numberPut a 1 in the corresponding position of the binary equivalentRepeat the subtract procedure with the remainder until the reminder becomes 0
Method 2: DivisionDivide the number by 2The remainder (either 0 or 1) gives the least significant bitDivide the quotient by 2 repeatedly until the quotient becomes 0
![Page 12: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/12.jpg)
12
Method 1: SubtractionN = 746The largest power of 2 that less than or equal to 746 is 512 (i.e. 29)Put a “1” in the 29 positionCompute remainder: 746 - 512 = 234The next smaller power of 2 is 256 (28), but that is larger than 234.So put a “0” in the 28 positionThe next smaller power of 2 is 128 (27), which is smaller than 234.So put a “1” in the 27 positionThe new remainder is 234 - 128 = 106
Answer: - - - - - - - - - -
Answer: 1 - - - - - - - - -
Answer: 10 - - - - - - - -
Answer: 101 - - - - - - -
![Page 13: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/13.jpg)
13
Method 1: Subtraction
And so on until the remainder becomes zeroFinally we will have (746)10
= 1 x 29 + 0 x 28 + 1 x 27 + 1 x 26 + 1 x 25 + 0x 24 + 1 x 23 + 0 x 22 + 1 x 21 + 0= (1011101010)2
Do you think it is easy to compute?
![Page 14: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/14.jpg)
14
Method 2: DivisionN = 746
746 / 2 = 373 + 0/2373 / 2 = 186 + 1/2186 / 2 = 93 + 0/293 / 2 = 46 + 1/246 / 2 = 23 + 0/223 / 2 = 11 + 1/211 / 2 = 5 + 1/25 / 2 = 2 + 1/22 / 2 = 1 + 0/21 / 2 = 0 + 1/2
?????????0????????10???????010??????1010?????01010????101010???1101010??11101010?0111010101011101010
quotientremainder
![Page 15: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/15.jpg)
15
Method 2 (Another Format)
010
0101010
01010101010
110101011101010
0111010101011101010
7 4 623 7 32 … 01 8 62 … 1
9 32 … 04 62 … 12 32 … 01 12 … 1
52 … 122 … 112 … 00 … 1
quotient remainder
MSB
LSB
![Page 16: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/16.jpg)
16
How About Decimal Factions?Multiply by 2 to give an integer and a fractionRepeat multiplying the fraction until it becomes 0e.g. convert decimal 0.6875 to binary
0.6875 x 2 = 1.37500.3750 x 2 = 0.75000.7500 x 2 = 1.50000.5000 x 2 = 1.0000(0.6875)10 = (0.1011)2
Integer and fractional parts is done separately(746.6875)10 = (1011101010.1011)2
MSB
LSB
![Page 17: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/17.jpg)
17
Method 1 vs. Method 2
Subtraction produces binary digits from the MSB to the LSB (from left to right)Division produces binary digits reversely, from the LSB to the MSB (from right to left)
Both methods work slowly, do we have faster methods?
![Page 18: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/18.jpg)
18
Octal NumbersOctal digits are: 0, 1, 2, … 7e.g. (1352)8
= 1 x 83 + 3 x 82 + 5 x 81 + 2 x 80
= 512 + 192 + 40 + 2= (746)10
To convert decimal to octalDivide the number by 8 repeatedlye.g. (746)10
746 / 8 = 93 … 293 / 8 = 11 … 511 / 8 = 1 … 31 / 8 = 0 … 1
Answer: (1352)8
![Page 19: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/19.jpg)
19
Octal FractionMultiply by 8 to give an integer and a fractionRepeat multiplying the fraction until it becomes 0Convert decimal 0.513 to a 3-digit octal fraction
0.513 x 8 = 4.1040.104 x 8 = 0.8320.832 x 8 = 6.6560.656 x 8 = 5.248…(0.513)10 = (0.4065…)2 = (0.407)2 (rounded)
MSB
LSB
![Page 20: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/20.jpg)
20
Octal NumbersBase 8, a power of 2 (23 = 8)Each octal digit corresponds to 3 binary digitsTo convert binary to octal
Partition the binary digits into groups of 3 bits eachNote the way of groupingAdd 0s to the MSB and LSB to make the no. of bits a multiple of 3e.g. (1 011 101 010 . 001 01)2 = (001 011 101 010 . 001 010)2= (1 3 5 2.1 2)8
![Page 21: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/21.jpg)
21
Octal Numbers
To convert octal to binaryReplace each octal digit by 3-bit binary equivalentFinally remove the extra 0s from MSB & LSBe.g. (1352.12)8
= (001 011 101 010 . 001 010)2
= (001 011 101 010 . 001 010)2
= (1 011 101 010 . 001 01)2
![Page 22: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/22.jpg)
22
Hex Conversion
Hex digits are: 0, 1, … 9, A, B, C, D, E, FA is 10B is 11C is 12D is 13E is 14F is 15
Each hex digit can be in the range 0 to 15
![Page 23: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/23.jpg)
23
Hex ConversionTo convert hex to decimal
e.g. (2EA)16= 2 x 162 + 14 x 161 + 10 x 160
= 512 + 224 + 10= (746)10
To convert decimal to hexDivide the number by 16 repeatedlye.g. (746)10
746 / 16 = 46 … 10 (A)46 / 16 = 2 … 14 (E)2 / 16 = 0 … 2 Answer: (2EA)16
![Page 24: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/24.jpg)
24
Hex ConversionTo convert binary to hex
Partition the binary digits into groups of 4 bits eachAdd necessary 0s to left and righte.g. (10 1110 1010)2 = (2 E A)16
To convert hex to binaryReplace each hex digit by 4-bit binary equivalent and remove the extra 0se.g. (2EA)16 = (0010 1110 1010)2
2 E A
![Page 25: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/25.jpg)
25
Method 3: Convert via Octal
Convert decimal to octal firstDivide by 8 repeatedly, and we have(746)10 = (1352)8
Now convert octal to binaryReplace each octal digit by 3-bit binary equivalent and remove the extra 0s(1352)8 = (1 011 101 010)2
![Page 26: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/26.jpg)
26
Or Via Hexadecimal
Convert decimal to hex firstDivide by 16 repeatedly, and we have(746)10 = (2EA)16
Now convert hex to binaryReplace each hex digit by 4-bit binary equivalent and remove the extra 0s(2EA)16 = (10 1110 1010)2
![Page 27: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/27.jpg)
27
Octal and HexadecimalCompact representation of binary numbersMore convenient for people than using bit strings (3 or 4 times longer)Computer manuals usually use octal or hexadecimal numbers to specify binary quantities
e.g. memory address of computer usually written in hex. FormatWhich one more easy to read? 2EA or 001011101010
![Page 28: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/28.jpg)
28
Binary Arithmetic
![Page 29: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/29.jpg)
29
Binary Addition
Four cases only0 + 0 = 00 + 1 = 11 + 0 = 11 + 1 = 10 (a carry of 1 to the next bit)
An XOR relation0 1
0 0 1
1 1 0The carry bit is omitted here
![Page 30: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/30.jpg)
30
Binary Addition: Example
Compute the sum of (0110)2 and (0111)2(0110)2 = (6)10
(0111)2 = (7)10
0 1 1 00 1 1 1+)
1
00 1 1 00 1 1 1+)
0 1
1 0Carries
Sum the 2nd LSBs and the carry bit: 0 + (1 + 1) = 0 + 10 = 10
0 1 1 00 1 1 1+)
1 0 1
1 1 00 1 1 00 1 1 1+)
1 1 0 1
1 1 0
The final result is 11012 (1310)
Sum the LSBsfirst: 0 + 1 = 1
Augend
Addend
Sum
![Page 31: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/31.jpg)
31
Binary Addition: 2nd Example
Compute the sum of (1101)2 and (0101)2(1101)2 = (13)10 ,(0101)2 = (5)10
Result should be (18)10
But…
1 1 0 10 1 0 1+)
1 0 0 1 0
1 1 0 1
Overflow (result out of range)
Addition of n-bit positive integers may produces (n+1)-bit result
0 0 1 02 = 210
1 0 0 1 02 = 1810
![Page 32: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/32.jpg)
32
One-bit adder
A complete table defining the addition process
a b cin cout s00001111
00011001
101010
1 1
0 00 10 11 00 11 01 01 1
0 1 1 00 1 1 1+)
1 1 0 1
1 1 0ab
s
Cout cin
The resulting carry from the addition
![Page 33: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/33.jpg)
33
Signed NumberUnsigned numbers
Either zero or positive integers, e.g. 5, 3, 0Signed numbers
An integer has a sign to indicate positive or negative numbers
Signed-magnitude, e.g. +5, -5, -3How about binary system?
The MSB be a sign bit (0 means +, 1 means -)i.e. +5 = 0101, -5 = 1101 (4-bit system, 3 bits for magnitude)
![Page 34: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/34.jpg)
34
Negative Binary NumberHow to compute the sum if they involve negative numbers?
e.g. assume 4-bit binary system is used, compute (+5) + (-3)+5 = 01012-3 = 10112Result is 100002 (overflow and incorrect!)
Another problem0000 (positive zero)1000 (negative zero)Any difference between them? Cause confusion!
![Page 35: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/35.jpg)
35
How to represent negative binary?
The MSB is still a sign bit Positive number, +a, is still stored as usual
e.g. +5 = 01012, 0 = 00002 (0 means positive)Negative number, -a, is stored as 2n - a
e.g. -3= 24 – 3= 16 – 3= 13= 11012
This is called two’s complement format
![Page 36: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/36.jpg)
36
Two’s ComplementThe largest number can be stored is 2n-1 - 1for n-bit binary system
e.g. 7 (i.e. 01112) for 4-bit binary
The smallest number can be stored is -2n-1
e.g. -8 (i.e. 10002) for 4-bit binary
Binary Unsigned Signed0000 0 0
1010 10 -6
1011 11 -5
… … …
0001 1 +1
0010 2 +2
0011 3 +3
0100 4 +4
0101 5 +5
0110 6 +6
0111 7 +7
1000 8 -8
1001 9 -7
1110 14 -2
15 -11111
![Page 37: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/37.jpg)
37
Find the Two’s Complement
Negative number, -a, is stored as 2n - aNot convenient to compute 2n – aAn easier way to find the 2’s complement
Step 1) Find the binary equivalent of the magnitudeStep 2) Complement each bit (i.e. 0’s to 1’s, 1’s to 0’s) (this is 1’s complement)Step 3) Add 1 (this is 2’s complement)
![Page 38: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/38.jpg)
38
3 Examples
0 1 0 1
1 0 1 0
+)
1 0 1 1
1
Complement the bits
Magnitude
= 5 =
Add 1
0 0 0 1
1 1 1 0
+)
1 1 1 1
1
a) -5 b) -1
0 0 0 0
1 1 1 1
+)
0 0 0 0
1
c) -0
The carry bit is ignored here
Find the 2’s complement of -5, -1 and 0
Magnitude
= 1 =
Magnitude
= 0 =
![Page 39: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/39.jpg)
39
Find the magnitude
Now giving a negative number that stored in 2’s complement format, how can you tell the magnitude?Two methods
1) reverse the process of 2’s complementi.e. Subtract 1, then complement
2) complement first, then add 1Addition is easier than subtraction
![Page 40: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/40.jpg)
40
Example: Find the magnitude
1 0 1 1
1-)
1 0 1 0
0 1 0 1
subtract 1
= (5)10
complement
-5 1 0 1 1
0 1 0 0
+)
0 1 0 1
1
complement
add 1
Method 1: Method 2:
Compute the magnitude of binary no. (1011)2 which is stored in 2’s complement format
-5
= (5)10
![Page 41: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/41.jpg)
41
1’s & 2’s ComplementFor a n-digit binary number,
The sum of the number and its 1’s complement will be 1…1 (n 1s) = 2n - 1The sum of the number and its 2’s complement will be 10…0 (n 0s) = 2n
e.g. -a is represented as 2n - a in 2’s complement format
a + -a = a + (2n - a)= 2n
![Page 42: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/42.jpg)
42
Back to the Addition Problem
Now we know how to represent negative binary numbersHow to do addition?
1 0 1 1
1 1 1 0
0 0 1 1
- 5
- 2
+ 3
Example 1:
1 0 1 1
1 0 0 0 0
0 1 0 1
- 5
0
+ 5
Example 2:
The carry outs are ignored
1 0 1 1
1 0 0 1 0
0 1 1 1
- 5
2
+ 7
Example 3:
![Page 43: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/43.jpg)
43
Overflow
There is a limitation in additionWhen the sum is out of range, we call it overflowe.g. For 4-bit system, sum = a + b
The range of sum: -8 ≤ sum ≤ +7Overflow when sum < -8, or sum > 7Note: overflow will occur even if a and b are in the range!
![Page 44: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/44.jpg)
44
Overflow Example0 1 0 1
1 0 0 1
0 1 0 0
+ 5
+ 9
+ 4-7?
Both +5, +4 are in the range
1 1 0 1
(1) 0 1 1 1
1 1 0 0
- 5
- 9
- 4+7?
Both -5, -4 are in the range
Overflow may occur if the two no. are of the same signBut addition of opposite signs will never produces overflow (why?)
![Page 45: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/45.jpg)
45
Binary Subtraction
Subtraction can be accomplished by 1) taking the 2’s complement of the 2nd
operand2) then perform addition
Work for both signed and unsigned numbersi.e. a – b is computed as a + (-b)
![Page 46: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/46.jpg)
46
Binary Subtraction Example
0 1 1 1
(1) 0 0 1 0
1 0 1 1
+ 7
+ 2
– 52’s complement
of +5
Compute 7 – 5:
Ignore the carry out
0 1 1 1
1 1 0 0
0 1 0 1
+ 7
+12
– – 52’s complement
of -5
Compute 7 – (–5):
-4? overflow
![Page 47: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/47.jpg)
47
Radix ComplementThere are 2 types of complement for any radix-rnumber system
r’s complement & (r-1)’s complementBinary system: 2’s & 1’s complementDecimal system: 10’s & 9’s complement
For a n-digit radix-r number systemThe (r-1)’s complement of number a is
r n - a - 1
The r’s complement of number a isr n - a
![Page 48: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/48.jpg)
48
ExampleGiven a decimal number 35, compute its 10’s and 9’s complement
a = 35Radix r = 10No. of digits n = 29’s complement = 102 - 35 - 1 = 6410’s complement = 102 - 35 = 65
![Page 49: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/49.jpg)
49
Subtraction with ComplementFor radix-r number system, the general form of subtraction is
a – b= a + (r’s complement of b)= a + (rn – b)= a – b + rn
If a > b, the sum is a positive numbera – b + rn = a – b if we discard the carry out (i.e. rn)
If a < b, the sum is a negative numbera – b + rn = rn – (b – a) = r’s complement of (b – a)
![Page 50: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/50.jpg)
50
Codes in Digital World
![Page 51: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/51.jpg)
51
Decimal Codes
Computer systems operate on binary no.Human use decimalWhen communicate with computer systems
Input: convert decimal no. to binaryProcess: perform arithmetic calculations in binaryOutput : convert binary result back to decimal
![Page 52: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/52.jpg)
52
Binary Coded Decimal (BCD)
Computer can accept binary values onlyMust represent the decimal digits by a code that contains 1s and 0sCalled binary coded decimal (BCD)Possible to perform arithmetic operations directly with BCD
![Page 53: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/53.jpg)
53
BCD Example739 would be stored as
011100111001 (12 bits in series)
There are several BCD codes8421 code5421 code2121 codeExcess 3 code2 of 5 codes
7 3 9Compare with the binary representation of 73910
= 10111000112
10 bits only!
![Page 54: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/54.jpg)
54
8421 Code8421 code is a kind of weighted codeUse 4-bit to represent a decimal digit8, 4, 2, 1 are the weight of the bits
Code: a3a2a1a0Value: (8 x a3) + (4 x a2) + (2 x a1)+ (1 x a0)
ExampleCode: 0110Value: (8 x 0) + (4 x 1) + (2 x 1)+ (1 x 0)= 4 + 2 = 6
![Page 55: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/55.jpg)
55
Three Weighted CodesValue
(decimal digit) 8421 code 5421 code 2421 code
0 0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
0000
10000
0001
0010
0011
0100
1000
1001
1010
1011
1100
0101
0110
0111
1101
1110
0001
2 0010
3 0011
4 0100
5 1011
6 1100
7 1101
8 1110
9 1111
0101
0110
0111
1000
1001
1111 1010
unused
The code are the same for the first 5 digits
(5 x 1) + (4 x 0) + (2 + 1) + (1 x 1)= 5 + 2 + 1= 8
(2 x 1) + (4 x 1) + (2 + 1) + (1 x 0)= 2 + 4 + 2= 8
4-bit codes have 16 combinations, but we just use 10 of them. The remaining 6 codes are unused (redundancy)
![Page 56: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/56.jpg)
56
2421 CodeA self-complementing code (9’s complement)
The complement of 0 is 9 (0000 ↔ 1111)The complement of 1 is 8 (0001 ↔ 1110)The complement of 2 is 7 (0010 ↔ 1101)The complement of 3 is 6 (0011 ↔ 1100)The complement of 4 is 5 (0100 ↔ 1011)
Evenly distribution of 1s and 0sEach bit has exactly five 1s and five 0s for the ten decimal values
![Page 57: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/57.jpg)
57
Excess 3 Code (XS3)Value
(decimal digit) 8421code Excess 3 code
0 0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
10011
0100
0101
0110
0111
1000
1001
1010
1011
1100
0000
0001
0010
1101
1110
23456789
unused
1111
Excess 3 code is a shifted 8421 code
XS3’s 0 = 8421 code’s 3
XS3’s 1 = 8421 code’s 4
…
Evenly distribution of 1s and 0s
8421 code: the MSB has eight 0s, only two 1s
XS3: each bit has exactly five 1s and five 0s
9’s complement code:Complement of 0 is 9 (0011 ↔ 1100)Complement of 1 is 8 (0100 ↔ 1011)…Complement of 4 is 5 (0111 ↔ 1000)
![Page 58: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/58.jpg)
58
2 of 5 CodeValue
(decimal digit) 2 of 5 Code
0 11000
10100
10010
10001
01100
01010
01001
00110
00101
00011
the rest of the 22
patterns with 0, 1, 3, 4 or 5 1’s
123456789
unused
Use 5 bits to represent each digit
exactly two 1s and three 0s for all code
Advantage: capable for error detection
If an error is made in just one of the bits during storage or transmission (the result will contain either one or three 1’s), can be detected as an error
Any limitation in error detection?
![Page 59: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/59.jpg)
59
BCD SummaryWeighted code
8421, 5421, 2124 codesNon-weighted code
XS3, 2 of 5 codesCode that self-complementing
2124, XS3 codesCode that able to detect error
2 of 5 codeCode that easy to determine negative number
5421, 2421, XS3 codes (why?)
![Page 60: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/60.jpg)
60
Determine Negative NumbersFor a signed 10’s complement (decimal) number, the first digit of that number would be in the range of 5 to 9 for negative numbersIn 5421, 2421 and XS3 codes, correspond to the first bit of the no. being 1
For these 3 codes, the first bit of no. 0 to 4 is “0”, while the first bit of no. 5 to 9 is “1”
Therefore, only check 1 bit can determine if it is a negative number
More complex logic is required for 8421, 2 of 5 codes
![Page 61: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/61.jpg)
61
BCD drawbacks
Inefficiency of storageUsed 10 out of 16 combinations (weighted codes and XS3 code)Used 10 out of 32 combinations (2 of 5 code)
Complex arithmetic calculation
![Page 62: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/62.jpg)
62
Example: 8421 Code Addition
0 1 1 1
1 1 0 1
0 1 1 0
7
1 3
6
The 8421 code representation of decimal 7 and 6
Compute 7 + 6:
1 1 0 1
1 0 0 1 1
0 1 1 0If the result is an unused code, add 110 to it
1
The resultant value is unused in 8421 code!
3Result is correct now
![Page 63: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/63.jpg)
63
Example: 8421 Code Addition
1 0 0 1
1 0 0 1 0
1 0 0 1
9
1 8
9
The 8421 code representation of decimal 9
Compute 9 + 9:
1 0 0 1 0
1 1 0 0 0
0 1 1 0Also add 110 to it
1
The resultant value has a carry out!
8Result is correct now
![Page 64: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/64.jpg)
64
ASCII CodeAmerican Standard Code for Information InterchangeTo store alphanumeric information
AlphaLetters: A, B, C, …, Z, a, b, c, …, zSymbols: ?, !, #, @, (, space, tab …
Numeric: 0, 1, 2, …, 9Control signals: carriage return, delete, Esc, break…
7-bit, to code various characters on standard keyboard
![Page 65: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/65.jpg)
65
100 1100 110 1111
110 0111 110 1001 110 0011
ASCII Code TableASCII Code Value
000 0000 NULL
… …
010 0000 Space
010 0001 ! (exclamation mark)
010 0010 “ (double quote)
… …
011 0000 0
011 0001 1
… …
011 1010 : (colon)
… …
100 0001 A
… …
101 1010 Z
… …
110 0001 a
… …
111 1010 z
… …
control signals
symbols
numeric characters
capital letters
symbols
small letters
symbols
symbols
The word Logic would be coded as:
L o
g i c
(Please refer to the complete ASCII table in your book)
011 0111 011 0011 011 1001
739 would be coded as:
7 3 9
![Page 66: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/66.jpg)
66
Bit Changes in CodeLook at the BCD code table, the no. of bits that change from one binary value to the next variesMultiple bit changes may not be good for some applicationsIn 1953, Frank Gray invented a code that has one-bit fixed difference between adjacent code word
Value 8421 code0 0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
123456789
1-bit changes
2-bit changes
3-bit changes
![Page 67: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/67.jpg)
67
Gray CodeNumber Gray code
0123456789101112131415
0000
0001
0011
0010
0110
0111
0101
0100
1100
1101
1111
1110
1010
1011
1001
1000
00000001
01110101
11101010
Consecutive numbers differ in one bit only, but can you memorize the pattern?
![Page 68: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/68.jpg)
68
Gray Code
A class of unit distance codeNo need to memorize the Gray code tableWe can compute the Gray code easily
Step 1) Convert to binary number firstStep 2) Add a leading 0 to the MSBStep 3) XOR each two adjacent bits
![Page 69: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/69.jpg)
69
Example: Gray Code
Find the Gray code of decimal value 13(1 3)10 = (1 1 0 1)2
(0 1 1 0 1)2
1 0 1 1
Convert to binary number
Add a leading 0 to the MSB
XOR each two adjacent bits
The Gray code of 13♁ ♁ ♁ ♁
![Page 70: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/70.jpg)
70
Example: Convert to Binary
Given a Gray code 10001011, find its representing decimal value
1 0 0 0 1 0 1 1
1 1 1 10 0
10
= (1 1 1 1 0 0 1 0)2 = (242)10
If the code has leading 0s, remove them
Set the output bit as 1
Starting from MSB, replace each bit in the Gray code to the output bit
If meet an one in the Gray code, complement the output bit
And continue to replace process
The representing decimal value
![Page 71: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/71.jpg)
71
Parity and Error Correction
![Page 72: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/72.jpg)
72
Data Communication
The transmission of data over a channel may result in error (e.g. interference, noise)Some schemes are developed to detect and/or correct the error(s)We have to insert extra bits in the data
![Page 73: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/73.jpg)
73
Parity Bit
The simplest method for error detectionAn additional bit is added, called parity bitThe value of parity bit is set to make the total no. of 1s in the resulting code word either even or odd
![Page 74: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/74.jpg)
74
Even / Odd Parity Bit
1000 10001 10000
1010 10100 10101
Original code word Even parity Odd parity
no. of 1s = 2 (even) no. of 1s = 3 (odd)no. of 1s = 2
Original Code: a1 a2 a3 a4 (4-bit)Even Parity bit a5 = a1 ♁ a2 ♁ a3 ♁ a4
Odd Parity bit a5 = a1 ♁ a2 ♁ a3 ♁ a4 ♁ 1Parity: a1 a2 a3 a4 a5 (5-bit)
![Page 75: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/75.jpg)
75
Error DetectionAssume even parity is usedIf the received code word is 01011Total no. of 1s in code word is 3 (odd no.)Conclusion: errors occurred during transmission (at least 1 bit has been changed)Limitation
Able to detect 1, 3 or any odd no. of error bitsBut an even no. of errors is undetected!
![Page 76: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/76.jpg)
76
Vertical Longitudinal Parity Bits
The previous method can detect error, but can’t correct errorBecause it cannot locate the error bitHow about if we have a two-dimension parity bits?
![Page 77: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/77.jpg)
77
Example: Error Correction1 0 0 01 0 1 00 1 1 01 0 1 1
1 0 0 0 11 0 1 0 00 1 1 0 01 0 1 1 1
The original block of code
Even parity
1 1 1 1 0
Parity bit for its row
Parity bit for its column
1 0 0 0 11 0 1 0 00 1 0 0 01 0 1 1 11 1 1 1 0
Received block of code
Error detected in this row (one 1s)
Error detected in this column (three 1s)
Error bit has been located!
1 0 0 0 11 0 1 0 00 1 1 0 01 0 1 1 11 1 1 1 0
Received block of code
Error corrected
![Page 78: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/78.jpg)
78
Limitation
Consume a lot of bits for error detection and correctionOnly work for single error in a row (or column)Able to detect double error in the same row (or columns), but not able to correct them (why?)
![Page 79: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/79.jpg)
79
Further Reading
Introduction to Logic DesignChapter 1.1 A brief review of number systems
Digital Logic Circuit Analysis & DesignChapter 1 Number Systems and Codes
![Page 80: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/80.jpg)
80
OBTL: upon completion of this lecture, you should able to …
express a radix-r number in the polynomial formperform number conversion between different radix systemsperform binary arithmeticunderstand the concept of complement numbers and signed numbers representationsexpress signed number in signed magnitude, 1's complement, and 2's complement formatsperform 2’s complement and 1’s complement number arithmeticexpress numbers in different BCD formatperform BCD addition and subtractionrecognize Gray codeexpress number in the ASCII formatunderstand the concept of error detection and error correctionderive and perform single error detection and single error correction using parity bits
Self evaluation: Please tick the items that you are able to complete
![Page 81: 01 Number Systems](https://reader037.fdocuments.us/reader037/viewer/2022100309/5524dd4a4a795998488b48c5/html5/thumbnails/81.jpg)
81
Level of achievementExcellent
Able to apply learned knowledge to solve unseen problems
WellAble to perform arithmetic operations on different number systemsAble to perform error detection and correction using parity bits
BasicUnderstand the concept of number systemsAble to apply correct formulae to convert numbers
Self evaluation: Please tick the appropriate box and mark the date of evaluation