Sie sind auf Seite 1von 81

EE2000 Logic Circuit Design

Number Systems & Arithmetic

1
Outline
„ Number systems
„ Conversion between different radix systems
„ Binary arithmetic
„ Complement of binary numbers
„ Signed number representations
„ Codes in Digital World
„ BCD
„ ASCII
„ Gray codes
„ Parity and error correction
2
Number Systems
„Decimal number system employed in
everyday arithmetic
„Represent numbers by strings of digits
called positional notation
„Digits – the Latin word for fingers
„an-1 an-2 … a2 a1 a0 (0 ≤ ai < 10)
„Each digit is associated to a value depends
on its position in the string

3
Polynomial Form of Integer
„ The no. can be expressed in power series
„ N = an-1rn-1 + an-2rn-2 + … + a2r2 + a1r + a0
„ N: the decimal value of the integer
„ n: number of digits
„ r: radix (base)
„ ai: coefficients (digit), 0 ≤ ai < r
„ e.g. (7672)10
„ 4 digits, so n = 4
„ decimal number, i.e. r = 10
„ ai: 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 4


Floating Point Number
„ an-1 an-2 … a2 a1 a0 . a-1 a-2 … a-(m-1) a-m (0 ≤ ai < 10)
„ “.” is the radix point
„ The 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 form


„ N = an-1rn-1 + … + a1r1 + a0r0 + a-1r -1 + … + a-mr -m
„ Number of digits = n + m
„ e.g. (767.2)10
„ n = 3, m = 1
„ N = 7 x 102 + 6 x 101 + 7 + 2 x 10-1

5
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

6
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)

7
Binary Number System
„ A base 2 system with two Decimal Binary
digits: 0 and 1 0 0
„ Expressed with a string of 1s 1 1
and 0s 2 10
„ The digits are called bits 3 11
(binary digits). 4 100
„ The left most bit called most 5 101
significant bit (MSB) 6 110
„ The right most bit called 7 111
least significant bit (LSB)
8
Radix Conversion
„Convert from binary to decimal
„e.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 2
n 0 1 2 3 4 … 20
2n 1 2 4 8 16 … 1,048,576
9
From Decimal to Binary
„Convert from decimal to binary
„e.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

10
Two Methods
„ Method 1: Subtraction
„ Subtract from the largest power of 2 that less than or
equal to that number
„ Put a 1 in the corresponding position of the binary
equivalent
„ Repeat the subtract procedure with the remainder
until the reminder becomes 0
„ Method 2: Division
„ Divide the number by 2
„ The remainder (either 0 or 1) gives the least
significant bit
„ Divide the quotient by 2 repeatedly until the quotient
becomes 0
11
Method 1: Subtraction
„ N = 746 Answer: - - - - - - - - - -
„ The largest power of 2 that less than or equal to 746 is
512 (i.e. 29)
„ Put a “1” in the 29 position
„ Compute remainder: 746 - 512 = 234 Answer: 1 - - - - - - - - -
„ The next smaller power of 2 is 256 (28), but that is larger
than 234.
„ So put a “0” in the 28 position Answer: 10 - - - - - - - -
„ The next smaller power of 2 is 128 (27), which is smaller
than 234.
„ So put a “1” in the 27 position Answer: 101 - - - - - - -
„ The new remainder is 234 - 128 = 106

12
Method 1: Subtraction
„And so on until the remainder becomes
zero
„Finally we will have (746)10
„= 1 x 29 + 0 x 28 + 1 x 27 + 1 x 26 + 1 x 25 + 0
x 24 + 1 x 23 + 0 x 22 + 1 x 21 + 0
„= (1011101010)2

„Do you think it is easy to compute?


13
Method 2: Division
quotient
remainder
„ N = 746
„ 746 / 2 = 373 + 0/2 ?????????0
„ 373 / 2 = 186 + 1/2 ????????10
„ 186 / 2 = 93 + 0/2 ???????010
„ 93 / 2 = 46 + 1/2 ??????1010
„ 46 / 2 = 23 + 0/2 ?????01010
„ 23 / 2 = 11 + 1/2 ????101010
„ 11 / 2 = 5 + 1/2 ???1101010
„ 5 / 2 = 2 + 1/2 ??11101010
„ 2 / 2 = 1 + 0/2 ?011101010
„ 1 / 2 = 0 + 1/2 1011101010
14
Method 2 (Another Format)
quotient remainder
2 7 4 6
2 3 7 3 … 0 LSB 0
2 1 8 6 … 1 10
2 9 3 … 0 010
2 4 6 … 1 1010
2 2 3 … 0 01010
2 1 1 … 1 101010
2 5 … 1 1101010
2 2 … 1 11101010
2 1 … 0 011101010
0 … 1 1011101010 15
MSB
How About Decimal Factions?
„ Multiply by 2 to give an integer and a fraction
„ Repeat multiplying the fraction until it becomes 0
„ e.g. convert decimal 0.6875 to binary
„ 0.6875 x 2 = 1.3750 MSB
„ 0.3750 x 2 = 0.7500
„ 0.7500 x 2 = 1.5000
„ 0.5000 x 2 = 1.0000 LSB
„ (0.6875)10 = (0.1011)2
„ Integer and fractional parts is done separately
„ (746.6875)10 = (1011101010.1011)2

16
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?

17
Octal Numbers
„ Octal digits are: 0, 1, 2, … 7
„ e.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 octal
„ Divide the number by 8 repeatedly
„ e.g. (746)10
„ 746 / 8 = 93 … 2
„ 93 / 8 = 11 … 5
„ 11 / 8 = 1 … 3 Answer: (1352)8
„1 / 8 = 0 … 1

18
Octal Fraction
„ Multiply by 8 to give an integer and a fraction
„ Repeat multiplying the fraction until it becomes 0
„ Convert decimal 0.513 to a 3-digit octal fraction
„ 0.513 x 8 = 4.104 MSB
„ 0.104 x 8 = 0.832
„ 0.832 x 8 = 6.656
„ 0.656 x 8 = 5.248
LSB
„…
„ (0.513)10 = (0.4065…)2 = (0.407)2 (rounded)

19
Octal Numbers
„ Base 8, a power of 2 (23 = 8)
„ Each octal digit corresponds to 3 binary digits
„ To convert binary to octal
„ Partition the binary digits into groups of 3 bits each
„ Note the way of grouping
„ Add 0s to the MSB and LSB to make the no. of bits a
multiple of 3
„ e.g. (1 011 101 010 . 001 01)2
„ = (001 011 101 010 . 001 010)2
„ = (1 3 5 2.1 2)8

20
Octal Numbers
„To convert octal to binary
„Replace each octal digit by 3-bit binary
equivalent
„Finally remove the extra 0s from MSB & LSB
„e.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

21
Hex Conversion
„Hex digits are: 0, 1, … 9, A, B, C, D, E, F
„A is 10
„B is 11
„C is 12
„D is 13
„E is 14
„F is 15
„Each hex digit can be in the range 0 to 15
22
Hex Conversion
„ To 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 hex
„ Divide the number by 16 repeatedly
„ e.g. (746)10
„ 746 / 16 = 46 … 10 (A)
„ 46 / 16 = 2 … 14 (E)
„ 2 / 16 = 0 … 2 Answer: (2EA)16

23
Hex Conversion
„To convert binary to hex
„Partition the binary digits into groups of 4 bits
each
„Add necessary 0s to left and right
„e.g. (10 1110 1010)2 = (2 E A)16
„To convert hex to binary
„Replace each hex digit by 4-bit binary
equivalent and remove the extra 0s
„e.g. (2EA)16 = (0010 1110 1010)2
2 E A
24
Method 3: Convert via Octal
„Convert decimal to octal first
„Divide by 8 repeatedly, and we have
„(746)10 = (1352)8
„Now convert octal to binary
„Replace each octal digit by 3-bit binary
equivalent and remove the extra 0s
„(1352)8 = (1 011 101 010)2

25
Or Via Hexadecimal
„Convert decimal to hex first
„Divide by 16 repeatedly, and we have
„(746)10 = (2EA)16
„Now convert hex to binary
„Replace each hex digit by 4-bit binary
equivalent and remove the extra 0s
„(2EA)16 = (10 1110 1010)2

26
Octal and Hexadecimal
„ Compact representation of binary numbers
„ More 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. Format
„ Which one more easy to read? 2EA or 001011101010

27
Binary Arithmetic

28
Binary Addition
„Four cases only
„0 + 0 = 0
„0 + 1 = 1
„1 + 0 = 1
„1 + 1 = 10 (a carry of 1 to the next bit)
„An XOR relation
0 1

0 0 1

1 1 0
29
The carry bit is omitted here
Binary Addition: Example
„Compute the sum of (0110)2 and (0111)2
„(0110)2 = (6)10
„(0111)2 = (7)10

Carries 0 10 110 110


Augend 0110 0110 0110 0110
Addend +) 0 1 1 1 +) 0 1 1 1 +) 0 1 1 1 +) 0 1 1 1
Sum 1 01 101 1101

Sum the LSBs Sum the 2nd LSBs and The final result
first: 0 + 1 = 1 the carry bit: 0 + (1 + 1) is 11012 (13
30 10)
= 0 + 10 = 10
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…
1101
1101
+) 0 1 0 1
0 0 1 02 = 210
10010 1 0 0 1 02 = 1810

Overflow (result out of range)

Addition of n-bit positive integers 31


may produces (n+1)-bit result
One-bit adder
„A complete table defining the addition
process
a b cin cout s
The resulting carry
from the addition 0 0 0 0 0
0 0 1 0 1
110 Cout cin 0 1 0 0 1
0110 a 0 1 1 1 0
1 0 0 0 1
+) 0 1 1 1 b
1 0 1 1 0
1101 s 1 1 0 1 0
1 1 1 1 1
32
Signed Number
„Unsigned numbers
„Either zero or positive integers, e.g. 5, 3, 0
„Signed numbers
„An integer has a sign to indicate positive or
negative numbers
„Signed-magnitude, e.g. +5, -5, -3
„How 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)
33
Negative Binary Number
„ How to compute the sum if they involve negative
numbers?
„ e.g. assume 4-bit binary system is used,
„ compute (+5) + (-3)
„ +5 = 01012
„ -3 = 10112
„ Result is 100002 (overflow and incorrect!)
„ Another problem
„ 0000 (positive zero)
„ 1000 (negative zero)
„ Any difference between them? Cause confusion!

34
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
35
Two’s Complement
„ The largest number Binary
0000
Unsigned
0
Signed
0
can be stored is 2n-1 - 1 0001 1 +1

for n-bit binary system 0010 2 +2


0011 3 +3
„ e.g. 7 (i.e. 01112) for 4- 0100 4 +4
bit binary 0101 5 +5
0110 6 +6
„ The smallest number 0111 7 +7
can be stored is -2n-1 1000 8 -8
1001 9 -7
„ e.g. -8 (i.e. 10002) for 4- 1010 10 -6
bit binary 1011 11 -5
… … …
1110 14 -2
1111 15 -1
36
Find the Two’s Complement
„Negative number, -a, is stored as 2n - a
„Not convenient to compute 2n – a
„An easier way to find the 2’s complement
„Step 1) Find the binary equivalent of the
magnitude
„Step 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)

37
3 Examples
Find the 2’s complement of -5, -1 and 0

a) -5 b) -1 c) -0

Magnitude Magnitude Magnitude


=5=0101 Complement =1=0001 =0=0000
the bits
1010 1110 1111
Add 1
+) 1 +) 1 +) 1
1011 1111 0000

The carry bit is


ignored here 38
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 complement
„i.e. Subtract 1, then complement
„2) complement first, then add 1
„Addition is easier than subtraction

39
Example: Find the magnitude
Compute the magnitude of binary no. (1011)2 which is
stored in 2’s complement format

Method 1: Method 2:

-5 1011 -5 1011
subtract 1 complement
-) 1 0100
add 1
1010 +) 1
complement
0101 0101
= (5)10 = (5)10

40
1’s & 2’s Complement
„For a n-digit binary number,
„The sum of the number and its 1’s
complement will be 1…1 (n 1s) = 2n - 1
„The 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

41
Back to the Addition Problem
„Now we know how to represent negative
binary numbers
„How to do addition?
Example 1: Example 2: Example 3:

-5 1011 -5 1011 -5 1011


+3 0011 +5 0101 +7 0111
-2 1110 0 10000 2 10010

42
The carry outs are ignored
Overflow
„There is a limitation in addition
„When the sum is out of range, we call it
overflow
„e.g. For 4-bit system, sum = a + b
„The range of sum: -8 ≤ sum ≤ +7
„Overflow when sum < -8, or sum > 7
„Note: overflow will occur even if a and b are in
the range!

43
Overflow Example
Both +5, +4 are +5 0101
in the range
+4 0100
-7?
+9 1001

Both -5, -4 are -5 1101


in the range
-4 1100
+7?
- 9 (1) 0 1 1 1

Overflow may occur if the two no. are of the same sign
But addition of opposite signs will never produces overflow (why?)
44
Binary Subtraction
„Subtraction can be accomplished by
„1) taking the 2’s complement of the 2nd
operand
„2) then perform addition
„Work for both signed and unsigned
numbers
„i.e. a – b is computed as a + (-b)

45
Binary Subtraction Example
Compute 7 – 5:
+7 0111
2’s complement
–5 1011 of +5
+ 2 (1) 0 0 1 0

Ignore the carry out


Compute 7 – (–5):
+7 0111
2’s complement
––5 0101 of -5
+12 1100
-4? overflow 46
Radix Complement
„ There are 2 types of complement for any radix-r
number system
„ r’s complement & (r-1)’s complement
„ Binary system: 2’s & 1’s complement
„ Decimal system: 10’s & 9’s complement
„ For a n-digit radix-r number system
„ The (r-1)’s complement of number a is
„rn - a - 1
„ The r’s complement of number a is
„rn - a

47
Example
„Given a decimal number 35, compute its
10’s and 9’s complement

„a = 35
„Radix r = 10
„No. of digits n = 2
„9’s complement = 102 - 35 - 1 = 64
„10’s complement = 102 - 35 = 65
48
Subtraction with Complement
„ For 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 number
„ a – b + rn = a – b if we discard the carry out (i.e. rn)
„ If a < b, the sum is a negative number
„ a – b + rn = rn – (b – a) = r’s complement of (b – a)

49
Codes in Digital World

50
Decimal Codes
„Computer systems operate on binary no.
„Human use decimal
„When communicate with computer
systems
„Input: convert decimal no. to binary
„Process: perform arithmetic calculations in
binary
„Output : convert binary result back to decimal

51
Binary Coded Decimal (BCD)
„Computer can accept binary values only
„Must represent the decimal digits by a
code that contains 1s and 0s
„Called binary coded decimal (BCD)
„Possible to perform arithmetic operations
directly with BCD

52
BCD Example
„739 would be stored as
„011100111001 (12 bits in series)
7 3 9
Compare with the binary
„There are several BCD codes representation of 73910
„8421 code = 10111000112

„5421 code 10 bits only!

„2121 code
„Excess 3 code
„2 of 5 codes
53
8421 Code
„8421 code is a kind of weighted code
„Use 4-bit to represent a decimal digit
„8, 4, 2, 1 are the weight of the bits
„Code: a3a2a1a0
„Value: (8 x a3) + (4 x a2) + (2 x a1)+ (1 x a0)
„Example
„Code: 0110
„Value: (8 x 0) + (4 x 1) + (2 x 1)+ (1 x 0)
„= 4 + 2 = 6
54
Three Weighted Codes
Value
8421 code 5421 code 2421 code
(decimal digit)
0 0000 0000 0000
1 0001 0001 0001
The code are the same for
2 0010 0010 0010
the first 5 digits
3 0011 0011 0011
4 0100 0100 0100
5 0101 1000 1011 (5 x 1) + (4 x 0) + (2 + 1) + (1 x 1)
6 0110 1001 1100 =5+2+1
=8
7 0111 1010 1101
8 1000 1011 1110 (2 x 1) + (4 x 1) + (2 + 1) + (1 x 0)
9 1001 1100 1111 =2+4+2
unused 1010 0101 0101 =8
1011 0110 0110
4-bit codes have 16 combinations,
1100 0111 0111 but we just use 10 of them. The
1101 1101 1000 remaining 6 codes are unused
1110 1110 1001 (redundancy)
55
1111 1111 1010
2421 Code
„ A 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 0s
„ Each bit has exactly five 1s and five 0s for the ten
decimal values

56
Excess 3 Code (XS3)
Value Excess 3
(decimal digit)
8421code
code Excess 3 code is a shifted 8421 code
0 0000 0011 XS3’s 0 = 8421 code’s 3
1 0001 0100
XS3’s 1 = 8421 code’s 4
2 0010 0101
3 0011 0110 …
4 0100 0111 9’s complement code:
5 0101 1000 Complement of 0 is 9 (0011 ↔ 1100)
6 0110 1001 Complement of 1 is 8 (0100 ↔ 1011)
7 0111 1010 …
8 1000 1011 Complement of 4 is 5 (0111 ↔ 1000)
9 1001 1100
unused 1010 0000 Evenly distribution of 1s and 0s
1011 0001
8421 code: the MSB has eight 0s,
1100 0010
only two 1s
1101 1101
1110 1110
XS3: each bit has exactly five 1s and
five 0s 57
1111 1111
2 of 5 Code
Value
2 of 5 Code
(decimal digit)
0 11000 Use 5 bits to represent each digit
1 10100 exactly two 1s and three 0s for all code
2 10010
3 10001
4 01100 Advantage: capable for error detection
5 01010 If an error is made in just one of the bits
6 01001 during storage or transmission (the result
7 00110 will contain either one or three 1’s), can
8 00101 be detected as an error
9 00011 Any limitation in error detection?
unused the rest of
the 22
patterns
with 0, 1, 3,
4 or 5 1’s
58
BCD Summary
„ Weighted code
„ 8421, 5421, 2124 codes
„ Non-weighted code
„ XS3, 2 of 5 codes
„ Code that self-complementing
„ 2124, XS3 codes
„ Code that able to detect error
„ 2 of 5 code
„ Code that easy to determine negative number
„ 5421, 2421, XS3 codes (why?)
59
Determine Negative Numbers
„ For a signed 10’s complement (decimal) number,
the first digit of that number would be in the
range of 5 to 9 for negative numbers
„ In 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
60
BCD drawbacks
„Inefficiency of storage
„Used 10 out of 16 combinations (weighted
codes and XS3 code)
„Used 10 out of 32 combinations (2 of 5 code)

„Complex arithmetic calculation

61
Example: 8421 Code Addition
Compute 7 + 6:
The 8421 code representation
7 0111 of decimal 7 and 6
6 0110
The resultant value is unused in
13 1101 8421 code!

1101
If the result is an unused code,
0110 add 110 to it
1 0011
Result is correct now
1 3
62
Example: 8421 Code Addition
Compute 9 + 9:
The 8421 code representation
9 1001 of decimal 9
9 1001
The resultant value has a carry
18 10010 out!

10010
Also add 110 to it
0110
11000
Result is correct now
1 8
63
ASCII Code
„ American Standard Code for Information
Interchange
„ To store alphanumeric information
„ Alpha
„ Letters: A, B, C, …, Z, a, b, c, …, z
„ Symbols: ?, !, #, @, (, space, tab …
„ Numeric: 0, 1, 2, …, 9
„ Control signals: carriage return, delete, Esc, break…
„ 7-bit, to code various characters on standard
keyboard
64
ASCII Code Table
ASCII Code Value
000 0000 NULL
control signals The word Logic would be
… … coded as:
010 0000 Space
010 0001 ! (exclamation mark)
100 1100 110 1111
symbols L o
010 0010 “ (double quote)
… … 110 0111 110 1001 110 0011
011 0000 0 g i c
011 0001 1 numeric characters
… …
011 1010 : (colon) 739 would be coded as:
… …
symbols
011 0111 011 0011 011 1001
100 0001 A
7 3 9
… … capital letters
101 1010 Z
… … symbols (Please refer to the complete
110 0001 a ASCII table in your book)
… … small letters
111 1010 z 65
… … symbols
Bit Changes in Code
„ Look at the BCD code table, Value 8421 code
the no. of bits that change 0 0000
1-bit changes
1 0001
from one binary value to the 2-bit changes
2 0010
next varies 3 0011
„ Multiple bit changes may not 4 0100
3-bit changes

be good for some applications 5 0101


6 0110
„ In 1953, Frank Gray invented 7 0111
a code that has one-bit fixed 8 1000
difference between adjacent 9 1001

code word

66
Gray Code
Number Gray code
0 0000 0000
1 0001 0001
2 0011
3 0010
4 0110
5 0111 0111
6 0101 0101 Consecutive numbers
7 0100 differ in one bit only,
8 1100 but can you memorize
9 1101 the pattern?
10 1111
11 1110 1110
12 1010 1010
13 1011
14 1001
15 1000 67
Gray Code
„A class of unit distance code
„No need to memorize the Gray code table
„We can compute the Gray code easily
„Step 1) Convert to binary number first
„Step 2) Add a leading 0 to the MSB
„Step 3) XOR each two adjacent bits

68
Example: Gray Code
„Find the Gray code of decimal value 13
„(1 3)10 = (1 1 0 1)2 Convert to binary number

„(0 1 1 0 1)2 Add a leading 0 to the MSB

XOR each two adjacent bits


♁ ♁ ♁ ♁
„ 1 0 1 1 The Gray code of 13

69
Example: Convert to Binary
„Given a Gray code 10001011, find its
representing decimal value
„1 0 0 0 1 0 1 1 If the code has leading 0s, remove them
Set the output bit as 1
„1 1 1 1 Starting from MSB, replace each bit in
the Gray code to the output bit
„ 00
If meet an one in the Gray code,
„ 1 complement the output bit

„ 0 And continue to replace process

„= (1 1 1 1 0 0 1 0)2 = (242)10
The representing decimal value 70
Parity and Error Correction

71
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

72
Parity Bit
„The simplest method for error detection
„An additional bit is added, called parity bit
„The value of parity bit is set to make the
total no. of 1s in the resulting code word
either even or odd

73
Even / Odd Parity Bit
Original code word Even parity Odd parity

1000 10001 10000

1010 10100 10101

no. of 1s = 2 no. of 1s = 2 (even) no. of 1s = 3 (odd)

Original Code: a1 a2 a3 a4 (4-bit)


Even Parity bit a5 = a1 ♁ a2 ♁ a3 ♁ a4
Odd Parity bit a5 = a1 ♁ a2 ♁ a3 ♁ a4 ♁ 1
Parity: a1 a2 a3 a4 a5 (5-bit) 74
Error Detection
„Assume even parity is used
„If the received code word is 01011
„Total 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 bits
„But an even no. of errors is undetected!
75
Vertical Longitudinal Parity Bits
„The previous method can detect error, but
can’t correct error
„Because it cannot locate the error bit
„How about if we have a two-dimension
parity bits?

76
Example: Error Correction
The original block of code Received block of code
1000 10001 Error corrected
1010 10100
0110 01100
1011 10111
11110

Even parity Received block of code


10001 10001 Error bit has been
10100 10100 located!
01100 Parity bit for 01000 Error detected in
10111 its row 10111 this row (one 1s)
11110 11110

Parity bit for its column Error detected in this 77


column (three 1s)
Limitation
„Consume a lot of bits for error detection
and correction
„Only 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?)

78
Further Reading
„Introduction to Logic Design
„Chapter 1.1 A brief review of number systems

„Digital Logic Circuit Analysis & Design


„Chapter 1 Number Systems and Codes

79
OBTL: upon completion of this
lecture, you should able to …
‰ express a radix-r number in the polynomial form
‰ perform number conversion between different radix systems
‰ perform binary arithmetic
‰ understand the concept of complement numbers and signed
numbers representations
‰ express signed number in signed magnitude, 1's complement, and
2's complement formats
‰ perform 2’s complement and 1’s complement number arithmetic
‰ express numbers in different BCD format
‰ perform BCD addition and subtraction
‰ recognize Gray code
‰ express number in the ASCII format
‰ understand the concept of error detection and error correction
‰ derive and perform single error detection and single error correction
using parity bits

Self evaluation: Please tick the items that you are able to complete 80
Level of achievement
‰Excellent
‰Able to apply learned knowledge to solve unseen
problems
‰Well
‰Able to perform arithmetic operations on different
number systems
‰Able to perform error detection and correction using
parity bits
‰Basic
‰Understand the concept of number systems
‰Able to apply correct formulae to convert numbers
Self evaluation: Please tick the appropriate box and mark the date of evaluation
81

Das könnte Ihnen auch gefallen