Binary code lecture2

Download Report

Transcript Binary code lecture2

COMPLEMENTS AND
CODING
Complements
Complements are used in digital computers for simplifying the
substraction operation and for logical manipulation.
There are two type of complements for each base –r system
(1) The r’s complement
(2) The (r-1)’s complement
(3) When the base is 2 means binary no. the complements are
(i) 2’s comlement
•
(ii)1’s complement
When the base is 10 means decimal no. the complements
are
(i) 10’s complement
(ii) 9’s complement
r’s complement
• Given a positive number N in base r with an
integer part of n digits, the r’s compliment of
N is defined as rn – N for N ≠ 0 and 0 for N =
0.
Examples
•
•
•
•
(i)The 10’s complement of (52520)10 is
•
•
•
(ii)The 10’s complement of (0.3267)10 is
No integer part, so 10n = 100 = 1.
1 – 0.3267 = 0.6733
The number of digits in the number is n = 5.
105 – 52520 = 47480.
(iii)The 10’s complement of (25.639)10 is
102 – 25.639 = 74.361.
(iv)The 2’s complement of (101100)2 is
•
26 – 101100 = (1000000 - 101100)2 = 010100.
(v)The 2’s complement of (0.0110)2 is
(1 – 0.0110)2 = 0.1010.
(r-1)’s complement
• Given a positive number N in base r with an
integer part of n digits and a fraction part of m
digit, the (r-1)’s compliment of N is defined as
rn – r − m – N.
Examples
•
•
(i)The 9’s complement of (52520)10 is
No fractional part, so 10− m = 100 = 1.
(105 – 1 – 52520) = 99999 – 52520 = 47479
•
•
•
(ii)The 9’s complement of (0.3267)10 is
No integer part, so 10n = 100 = 1
(1 – 10− 4 − 0.3267) = 0.9999 – 0.3267 = 0.6732.
•
•
(iii)The 9’s complement of (25.639)10 is
(102 – 10− 3 – 25.639) = 99.999 – 25.639 = 74.360.
•
•
(iv) The 1’s complement of (101100)2 is
(26 – 1) – (101100) = (111111 – 101100)2 = 010011.
•
•
(v)The 1’s complement of (0.0110)2 is
(1 – 2− 4)10 – (0.0110)2 = (0.1111 – 0.0110)2 = 0.1001.
Subtraction with r’s
Complement
•
The subtraction of two positive numbers (M - N), both of base r,
may be done follows:
1.
Add the minuend M to the r’s complement of the subtrahend N.
2.
Insert the result obtained in step 1 for an end carry:
(a). If an end carry occurs, discard it.
(b). If an end carry does not occur, take the r’s complement
of the number obtained in the step 1 and place a negative
sign in front.
Examples
• Question 1:
Using 10’s complement, subtract 72532 – 3250.
• Solution:
M = 72532
N = 03250
72532
+
10’s complement of N = 96750
end carry
Answer: 69282
1
96750
69282
Examples
• Question 2:
Subtract (3250 – 72532)10.
.
• Solution:
M = 03250
N = 72532
03250
+
10’s complement of N = 27468
no carry
Answer: - 69282
27468
30718
Examples
• Question 3:
Use 2’s complement to perform M – N with given binary
numbers.
.
(a).
M = 1010100
N = 1000100
1010100
+
2’s complement of N = 0111100
end carry
Answer: 10000
1
0111100
0010000
Examples
(b).
M = 1000100
N = 1010100
2’s complement of N = 0101100
no carry
Answer: - 10000
1000100
+
0101100
1110000
Subtraction with (r – 1)’s
complement
•
The subtraction of M – N, both positive numbers in base r, may be
calculated in the following in the following manner:
1.
Add the minuend M to the (r -1)’s complement of the subtrahend N.
2.
Inspect the result obtained in the step 1 for an carry.
(a). If an end carry occurs, add 1 to te least significant digit (end-around
carry).
(b). If an end carry does not occur, take the (r-1)’s complement of the
number obtained in step 1 and place a negative sign in front.
Examples
• Question 1:
Using 9’s complement, subtract 72532 – 3250.
• Solution:
M = 72532
N = 03250
72532
+
9’s complement of N = 96749
end - around carry
96749
1
69281
+
Answer: 69282
1
69282
Examples
• Question 2:
Subtract (3250 – 72532)10.
.
• Solution:
M = 03250
N = 72532
03250
+
9’s complement of N = 27467
no carry
Answer: - 69282
27467
30717
Examples
• Question 3:
Use 1’s complement to perform M – N with given binary numbers.
.
(a).
M = 1010100
N = 1000100
1010100
+
1’s complement of N = 0111011
end - around carry
1
0111011
0010000
+
Answer: 10000
1
0010000
Examples
(b).
M = 1000100
N = 1010100
1’s complement of N = 0101011
no carry
Answer: - 10000
1000100
+
0101011
1110000
Binary code
• Digital data is represented, stored and transmitted as group of binary
bits.
• This group is called binary code.
• The binary code can be used for represent the number as well as alpha
numeric letters.
Classification of
binary code
Binary code
Weighted code
Binary
Ex: 0s1
Non weighted code
Reflective code
Sequential code
Error detecting and
correcting code
•Example:
Gray
Excess-3
Five bit BCD
•Example:
5211
2421
Excess-3
•Example:
8421
Excess-3
•Example:
Hamming
Parity
• BCD
Ex:
8421
2421
5211
4221
Binary code decimal
(BCD)
• We know that decimal, octal, hexadecimal can be represented
by binary digit.
• Not only numbers but letters and other symbols can be
represented by 1s 0s.
• Combination of binary digits that represent all these things are
called digital codes.
Binary code decimal
(BCD)
8421 (BCD)
Decimal
0000
0
0001
1
0010
2
0011
3
0100
4
0101
5
0110
6
0111
7
1000
8
1001
9
Gray code
• It is called cyclic or reflected code.
• In this code each code group does not differ from its
neighbour in more than one bit.
• This code is used for input and output devices in
digital system.
Table of gray code
Decimal number
Gray code
0
0 0 0 0
1
0 0 0 1
2
0 0 1 1
3
0 0 1 0
4
0 1 1 0
5
0 1 1 1
6
0 1 0 1
7
0 1 0 0
8
1 1 0 0
9
1 1 0 1
10
1 1 1 1
11
1 1 1 0
12
1 0 1 0
Conversion from
binary to gray code
Method:
•
Write down binary form of the given decimal number.
•
Write MSB as such.
•
Then add the binary digit from left to right at the adjacent
position.
•
Discard carry if any.
•
Write the digit which comes after addition.
Conversion from
binary to gray code
• Example:
Convert 15 into gray code.
Solution: (15)10 = (1111)2
We have to convert (1111)2 into gray code
Left most bit
1
1
+
1
0
+
1
0
+
1
0
Hence the resultant gray code is 1000 for 15.
Example
–
Convert (111011)2 into gray code.
Solution:
Left most bit
1
1
1.
2.
3.
+
1
0
+
1
0
+
+
+
0
1
1
1
1
0
Write the left most bit as such.
Then add the binary digit from left to right at the adjacent position.
Write the digit which comes after addition.
Hence the grey code of (111011)2 is 100110.
Conversion from grey
code to binary code
•
Method:
1.
Write the given grey code.
2.
Write the left most bit as such.
3.
Add this bit to the second left most bit, write the result,
discard carry.
4.
Add this result to the next left most bit diagonally.
Conversion from grey
code to binary code
• Example:
Convert the following grey code into binary code from (1 0 0 0).
Solution:
We have Left most bit
1
0
+
1
1
+
0
+
1
Hence the corresponding binary number us (1111)2.
0
1