ele2120 digital circuits and systems - cuhk …qzhao/ele2120_files/ele2120_tuto1.pdf1s complement...
TRANSCRIPT
![Page 1: ELE2120 Digital Circuits and Systems - CUHK …qzhao/ELE2120_files/ELE2120_tuto1.pdf1s complement & 2s complement To do 1s complement for binary numbers, reverse “1” to be “0”](https://reader033.vdocuments.site/reader033/viewer/2022051307/5abd65587f8b9ab02d8b8b58/html5/thumbnails/1.jpg)
The Chinese University of Hong Kong
ELE2120 Digital Circuits and Systems
Tutorial Note 1
Prepared by Wilson YU
![Page 2: ELE2120 Digital Circuits and Systems - CUHK …qzhao/ELE2120_files/ELE2120_tuto1.pdf1s complement & 2s complement To do 1s complement for binary numbers, reverse “1” to be “0”](https://reader033.vdocuments.site/reader033/viewer/2022051307/5abd65587f8b9ab02d8b8b58/html5/thumbnails/2.jpg)
Outline
1. Convert base-r system to Decimal system1. Convert base-r system to Decimal system
2. Convert Decimal system to base-r system2. Convert Decimal system to base-r system
3. 1s and 2s complement3. 1s and 2s complement
4. Error Detection – Gray code & Parity Codes4. Error Detection – Gray code & Parity Codes
3. Conversion between base-2, 8 and 16 system3. Conversion between base-2, 8 and 16 system
![Page 3: ELE2120 Digital Circuits and Systems - CUHK …qzhao/ELE2120_files/ELE2120_tuto1.pdf1s complement & 2s complement To do 1s complement for binary numbers, reverse “1” to be “0”](https://reader033.vdocuments.site/reader033/viewer/2022051307/5abd65587f8b9ab02d8b8b58/html5/thumbnails/3.jpg)
Convert base-r system to Decimal system
A decimal number such as 168.34 represents a quantity equal to 1 hundred, plus 6 tens, plus 8 units, plus 3 one tenths, plus 4 one hundredths.i.e.
In general, a number expressed in a base-r system has coefficients multiplied by powers of r:i.e.
2101210 104103108106101)34.168( −− ×+×+×+×+×=
102
21
1
00
11
22
11
210121
)...
...(
).......(
mm
nn
nn
rmnn
rarara
rarararara
aaaaaaaa
−−
−−
−−
−−
−−−−
×++×+×+
×+×+×++×+×=---(1)
The base of the number
![Page 4: ELE2120 Digital Circuits and Systems - CUHK …qzhao/ELE2120_files/ELE2120_tuto1.pdf1s complement & 2s complement To do 1s complement for binary numbers, reverse “1” to be “0”](https://reader033.vdocuments.site/reader033/viewer/2022051307/5abd65587f8b9ab02d8b8b58/html5/thumbnails/4.jpg)
Binary to Decimal Number
ExerciseConvert (1011.1)2 to decimal Number
SolutionStep 1: Find the base of Binary number, r = 2.Step 2: Substitute r = 2 and the coefficient into (1)Step 3: The following equation would be found.
Step 4: (1011.1)2= (11.5)10
1010123
2 )2121212021()1.1011( −×+×+×+×+×=
![Page 5: ELE2120 Digital Circuits and Systems - CUHK …qzhao/ELE2120_files/ELE2120_tuto1.pdf1s complement & 2s complement To do 1s complement for binary numbers, reverse “1” to be “0”](https://reader033.vdocuments.site/reader033/viewer/2022051307/5abd65587f8b9ab02d8b8b58/html5/thumbnails/5.jpg)
Hexadecimal to Decimal Number
ExerciseConvert (19.C)16 to decimal Number
SolutionStep 1: Find the base of Binary number, r = 16.Step 2: Substitute r = 16 and the coefficient into (1)Step 3: The following equation would be found.
Step 4: (19.C)16= (25.75)10
10101
16 )1612169161().19( −×+×+×=C
For Hexadecimal Number, A = 10, B = 11, C =12D = 13, E = 14, F = 15
![Page 6: ELE2120 Digital Circuits and Systems - CUHK …qzhao/ELE2120_files/ELE2120_tuto1.pdf1s complement & 2s complement To do 1s complement for binary numbers, reverse “1” to be “0”](https://reader033.vdocuments.site/reader033/viewer/2022051307/5abd65587f8b9ab02d8b8b58/html5/thumbnails/6.jpg)
Convert Decimal system to base-r system
ExerciseConvert (25.75)10 to binary number
SolutionStep 1: Convert the integer into binary number.
10011
22222512631
Put the integer here
÷
Remainder of 25÷2
Quotient of 25÷2∴(25)10=(11001)2
![Page 7: ELE2120 Digital Circuits and Systems - CUHK …qzhao/ELE2120_files/ELE2120_tuto1.pdf1s complement & 2s complement To do 1s complement for binary numbers, reverse “1” to be “0”](https://reader033.vdocuments.site/reader033/viewer/2022051307/5abd65587f8b9ab02d8b8b58/html5/thumbnails/7.jpg)
Convert Decimal system to base-r system
ExerciseConvert (25.75)10 to binary number
SolutionStep 2: Convert the fraction into binary number.
Step 3: Combine the integer and factional part.(25.75)10 = (11001.11)2
0
0.5Fraction
>>>
>>>
a-2=1
a-1=1Coefficient
+1=0. 5×2
+1=0.75×2Integer
![Page 8: ELE2120 Digital Circuits and Systems - CUHK …qzhao/ELE2120_files/ELE2120_tuto1.pdf1s complement & 2s complement To do 1s complement for binary numbers, reverse “1” to be “0”](https://reader033.vdocuments.site/reader033/viewer/2022051307/5abd65587f8b9ab02d8b8b58/html5/thumbnails/8.jpg)
Convert Decimal system to base-r system
ExerciseConvert (25.75)10 to hexadecimal number
SolutionStep 1: Convert the integer into binary number.
91
16251
Put the integer here
÷
Remainder of 25÷2
Quotient of 25÷2∴(25)10=(19)16
![Page 9: ELE2120 Digital Circuits and Systems - CUHK …qzhao/ELE2120_files/ELE2120_tuto1.pdf1s complement & 2s complement To do 1s complement for binary numbers, reverse “1” to be “0”](https://reader033.vdocuments.site/reader033/viewer/2022051307/5abd65587f8b9ab02d8b8b58/html5/thumbnails/9.jpg)
Convert Decimal system to base-r system
ExerciseConvert (25.75)10 to hexadecimal number
SolutionStep 2: Convert the fraction into binary number.
Step 3: Combine the integer and factional part.(25.75)10 = (19.C)16
0Fraction
>>> a-1=12(C)Coefficient
+12=0.75×16Integer
∴(0.75)10=(0.C)16
![Page 10: ELE2120 Digital Circuits and Systems - CUHK …qzhao/ELE2120_files/ELE2120_tuto1.pdf1s complement & 2s complement To do 1s complement for binary numbers, reverse “1” to be “0”](https://reader033.vdocuments.site/reader033/viewer/2022051307/5abd65587f8b9ab02d8b8b58/html5/thumbnails/10.jpg)
Conversion between base-2, 8 or 16 system
To Convert Binary number to Octal number.E.g. (1001100111.01011)2
Step 1: Divide the number into group with 3 numbers. And add some zeros at both ends if necessary.
(001 001 100 111. 010 110)2
Step 2: Convert each group to octal number.(1147.26)8
To Convert Octal number to Binary numberE.g. (234.54)8
Step 1: Convert each number to 3-digit binary numbers.(010 011 100. 101 100)2
Step 2: Cancel the redundant zeros.(10011100.1011)2
![Page 11: ELE2120 Digital Circuits and Systems - CUHK …qzhao/ELE2120_files/ELE2120_tuto1.pdf1s complement & 2s complement To do 1s complement for binary numbers, reverse “1” to be “0”](https://reader033.vdocuments.site/reader033/viewer/2022051307/5abd65587f8b9ab02d8b8b58/html5/thumbnails/11.jpg)
Conversion between base-2, 8 or 16 system
To Convert Binary number to Hexadecimal number.E.g. (1001100111.01011)2
Step 1: Divide the number into group with 4 numbers. And add some zeros at both ends if necessary.
(0010 0110 0111. 0101 1000)2
Step 2: Convert each group to hexadecimal number.(267.58)16
To Convert Hexadecimal number to Binary numberE.g. (1CD.EF)16
Step 1: Convert each number to 4-digit binary numbers.(0001 1100 1101. 1110 1111)2
Step 2: Cancel the redundant zeros.(111001101.11101111)2
![Page 12: ELE2120 Digital Circuits and Systems - CUHK …qzhao/ELE2120_files/ELE2120_tuto1.pdf1s complement & 2s complement To do 1s complement for binary numbers, reverse “1” to be “0”](https://reader033.vdocuments.site/reader033/viewer/2022051307/5abd65587f8b9ab02d8b8b58/html5/thumbnails/12.jpg)
1s complement & 2s complement
To do 1s complement for binary numbers, reverse “1” to be “0” and “0” to be “1” for each bit.
Example 1s complement of (1101100)2= (0010011)2
To do 2s complement for binary numbers, do 1s complement and then add 1.
Example2s complement of (1101100)2= (0010011)2 + 1= (0010100)2
![Page 13: ELE2120 Digital Circuits and Systems - CUHK …qzhao/ELE2120_files/ELE2120_tuto1.pdf1s complement & 2s complement To do 1s complement for binary numbers, reverse “1” to be “0”](https://reader033.vdocuments.site/reader033/viewer/2022051307/5abd65587f8b9ab02d8b8b58/html5/thumbnails/13.jpg)
2s complement
2s complement could be used to represent signed numbers.
Example-7= 2s complement of 7= 2s complement of (0111)2
= (1000)2 + 1= (1001)2
For 4-bit signed numbers, only -8 <= x <= 7 could be presented.
![Page 14: ELE2120 Digital Circuits and Systems - CUHK …qzhao/ELE2120_files/ELE2120_tuto1.pdf1s complement & 2s complement To do 1s complement for binary numbers, reverse “1” to be “0”](https://reader033.vdocuments.site/reader033/viewer/2022051307/5abd65587f8b9ab02d8b8b58/html5/thumbnails/14.jpg)
2s complement
By 2s complement, we could do subtraction by adding the 2s complement of the second operand.
i.e. a – b = a + (-b)Example
15 - 9=15 + (-9)= (01111)2 + (10111)2
= (00110)2
2s complement of 9
01111+10111---------
(1)00110
Carry could be ignored.
![Page 15: ELE2120 Digital Circuits and Systems - CUHK …qzhao/ELE2120_files/ELE2120_tuto1.pdf1s complement & 2s complement To do 1s complement for binary numbers, reverse “1” to be “0”](https://reader033.vdocuments.site/reader033/viewer/2022051307/5abd65587f8b9ab02d8b8b58/html5/thumbnails/15.jpg)
2s complement
Detect overflowIf two 2s complement numbers add together, overflow occurs when the sum is out of range.Example: 5+6 = 11 > 7
How do you know whether overflow occur?By observing the solution, we will know that two positive numbers add together will not equal to a negative number. So, overflow occur.
0101+0110
-----------(0)1011Carry is ignored!
![Page 16: ELE2120 Digital Circuits and Systems - CUHK …qzhao/ELE2120_files/ELE2120_tuto1.pdf1s complement & 2s complement To do 1s complement for binary numbers, reverse “1” to be “0”](https://reader033.vdocuments.site/reader033/viewer/2022051307/5abd65587f8b9ab02d8b8b58/html5/thumbnails/16.jpg)
2s complement
ExerciseCompute (1000)2 + (1100)2, see whether overflow occurs.
SolutionTwo negative numbers add together will not equal to positive number, so overflow occurs. 1000
+1100---------10100
![Page 17: ELE2120 Digital Circuits and Systems - CUHK …qzhao/ELE2120_files/ELE2120_tuto1.pdf1s complement & 2s complement To do 1s complement for binary numbers, reverse “1” to be “0”](https://reader033.vdocuments.site/reader033/viewer/2022051307/5abd65587f8b9ab02d8b8b58/html5/thumbnails/17.jpg)
Error Detection - Gray Code
0000000100110010011001110101010011001101111111101010101110011000
000001011010110111101100
00011110
01
4bit3bit2 bit1bit
2. flip
4. Put “1”s here
3. Put “0”s here
1 . Copy the pattern from the previous bits
![Page 18: ELE2120 Digital Circuits and Systems - CUHK …qzhao/ELE2120_files/ELE2120_tuto1.pdf1s complement & 2s complement To do 1s complement for binary numbers, reverse “1” to be “0”](https://reader033.vdocuments.site/reader033/viewer/2022051307/5abd65587f8b9ab02d8b8b58/html5/thumbnails/18.jpg)
Error Detection - Parity Codes (even)
For even parity, put an extra bit called parity bit to make the sum of bits always even.
Detect error if sum ≠even.
0000
1100
0110
1010
Parity bit(even)
Bit 0Bit 1Bit 2
![Page 19: ELE2120 Digital Circuits and Systems - CUHK …qzhao/ELE2120_files/ELE2120_tuto1.pdf1s complement & 2s complement To do 1s complement for binary numbers, reverse “1” to be “0”](https://reader033.vdocuments.site/reader033/viewer/2022051307/5abd65587f8b9ab02d8b8b58/html5/thumbnails/19.jpg)
Error Detection - Parity Codes (odd)
For odd parity, put an extra bit called parity bit to make the sum of bits always odd.
Detect error if sum ≠odd.
1000
0100
1110
0010
Parity bit(odd)
Bit 0Bit 1Bit 2
![Page 20: ELE2120 Digital Circuits and Systems - CUHK …qzhao/ELE2120_files/ELE2120_tuto1.pdf1s complement & 2s complement To do 1s complement for binary numbers, reverse “1” to be “0”](https://reader033.vdocuments.site/reader033/viewer/2022051307/5abd65587f8b9ab02d8b8b58/html5/thumbnails/20.jpg)
Challenge Exercise
Astronauts on Mars discovered a Martian equation
5x2-50x+125=0
with the given solutions x = 5 and x = 8.Deduce, from this equation, how many fingers do Martian’s have?Hint: they have an odd number of fingers.
![Page 21: ELE2120 Digital Circuits and Systems - CUHK …qzhao/ELE2120_files/ELE2120_tuto1.pdf1s complement & 2s complement To do 1s complement for binary numbers, reverse “1” to be “0”](https://reader033.vdocuments.site/reader033/viewer/2022051307/5abd65587f8b9ab02d8b8b58/html5/thumbnails/21.jpg)
Challenge Exercise
Astronauts on Mars discovered a Martian equation5x2-50x+125=0
with the given solutions x = 5 and x = 8.Deduce, from this equation, how many fingers do Martian’s have?Hint: they have an odd number of fingers.
Solution:Step 1: Let the base of the number system be r.Step 2: Convert the base-r number system into decimal number system. And substitute the root into the equation. Then, find the solution of the equation.
i.e.
So, Martian has 13 fingers.1013013023
0)5()2()1()5)(05()5)(5(2
012001200
===+−
=×+×+×+××+×−××
rorrrr
rrrrrrrr