Digital Electronics
L3
Outline
Signed Numbers
Complements
1’s and 2’s Complements of Binary Numbers
Arithmetic Operations with Signed Numbers
Codes
Gray Codes and its conversion
1
Bit combinations
Bit - a binary digit 0 or 1
Nibble - a group of four bits
Byte - a group of eight bits
Word - a group of sixteen bits;
2
Signed Numbers
Digital systems, such as the computer, must be able to handle both
positive and negative numbers.
A signed binary number consists of both sign and
magnitude information.
The sign indicates whether a number is positive or negative.
Magnitude is the value of the number.
There are 3 forms in which signed integer numbers can be
represented in binary:
Sign-magnitude
1’s complement
2’s complement
The 2’s complement is the most important and the sign magnitude
are rarely used. 3
Number Ranges
A signed number has the range:
-2n-1 to 2n-1 –1
An unsigned number has the range:
0 to 2n -1
4
Sign-Magnitude Form
The left most number in a signed binary number is the sign
bit,
which tells you whether the number is positive or negative.
0 is for positive, and 1 is for negative
The remaining bits are magnitude bits.
The magnitude bits are in true (uncomplemented) binary for
both positive and negative number
+25: 00011001 8-bit signed binary number
-25: 10011001
5
The Decimal Value of Signed Number
Sign-Magnitude
Only the magnitude bits are considered for finding the
decimal value, sign bit will decide the sign
Ex: 10010101
Considering the magnitude bits only: 0010101 :: 2110
The sign bit is 1; therefore the decimal number is -21
6
Compliments
Complements are used in digital computers for simplifying the
subtraction operations and for logical manipulation.
Easily represent a minus sign electronically in a computer.
Arithmetic operations are manageable.
There are two types of compliments for each number base
system.
(i) r’s complement (radix complement)
(ii) (r-1)’s complement (diminished radix complement)
For base 2, we have 2’s complement & 1’s complement.
7
(r-1)’s Compliments (DRC)
In general if given a number N in base r and having n digits, then its (r-1)’s
complement is defined as (rn-1)-N.
Binary case: r=2 & r-1 =1, so 1’s complement of N is (2n-1)-N.
Now 2n in binary is represented with 1 followed by n 0’s. Thus 2n-1 is
represented by n 1’s.
Example if n=4,
We have 24 =(10000)2 and 24 -1=(01111)2
Thus 1’s complement is obtained by subtracting each digit from 1. so
only 2 cases occurs either 1-0=1 or 1-1=0. So the bits changes from 0
to 1 & vice versa.
Example find 1’s comp. of (1010)2
1111-1010=0101 (1’s comp.) 8
(r-1)’s Compliments
(r-1)’s Compliments is obtained by subtracting each digit from (rn-1).
For Octal subtract each digit from 7.
For Hexadecimal subtract each digit from F (15).
For r=10,subtract each digit from 9.
9
r’s Compliments
In general if given a number N in base r and having n digits,
then its r’s complement is defined as: (rn-N) for N≠0.
: 0 for N=0.
Range of 2’s complement is: (-2n-1 to 2n-1-1).
• For 4 variable it is -8 to 7.
10
1’s & 2’s Complement of Binary Numbers
The 1’s complement and the 2’s complement of a binary
number are important because they permit the
representation of negative numbers.
1’s Complement of a Binary Number
The 1’s complement of a binary number is found by changing
all 1s to 0s and all 0s to 1s
10110010
01001101
11
2’s Complement of a Binary Number
The method of 2’s complement arithmetic is commonly used in
computers to handle negative numbers.
The 2’s complement of a binary number is found by adding 1 to
the LSB of the 1’s complement.
Q: Find the 2’s complement of 10110010
1 0 1 1 0 0 1 0 Binary number
0 1 0 0 1 1 0 1 1’s complement
+ 1 Add 1
0 1 0 0 1 1 1 0 2’s complement
12
1’s Complement Form
Positive numbers in 1’s complement form are represented the
same way as the positive sign magnitude number.
Negative numbers, however, are the 1’s complement of the
corresponding positive numbers
+25: 00011001
-25: 11100110
13
2’s Complement Form
Positive numbers in 2’s complement form are represented the
same way as the sign magnitude and 1’s complement forms.
Negative numbers, are the 2’s complement of the corresponding
positive numbers:
+25: 00011001
-25: 11100111
Q: Express the decimal number -39 as an 8-bit number in the
sign-magnitude, 1’s complement, and 2’s complement forms.
A: 8-bit number for +39: 00100111
Sign-magnitude form for -39: 10100111
1’s complement form for -39: 11011000
2’s complement form for -39: 11011001
14
Binary subtraction using 1’s Complement
Direct binary sub. becomes complicated as no. size increase.
Example: Subtract B from A, then it can be written as A-B or A+(-
B).
Step1: Convert no. to be subtrated in its 1’s complement from.
Step2: Perform the addition.
Step3: If the final carry (FC) is 1, then add it to the result obtained in
step 2. If the FC is 0, then result obtained in step 2 is –ve & in the 1’s
complement form.
Note: 1’s complement of B is (-B)
15
Subtraction with 1’s Complement
To subtract smaller no. with larger no. method is as follows:
(1) Determine the 1’s Complement of smaller no.
(2) Add this to larger no.
(3) Remove the carry & add it to the result. This carry is called EAC
(end-around carry)
Q.) Using direct & 1’s complement method, Subtract (1010)2
from (1111)2
Direct Method 1’s Complement Method
1111 1111
-1010 0101 (1’s complement)
0101 Carry 1 0100
Add Carry 1
0101
16
Binary subtraction using 2’s Complement
EAC is the drawback of 1’s comp.
Example: Subtract B from A, then it can be written as A-B or A+(-
B).
Step1: Find 2’s comp. of the no. to be subtrated.
Step2: Perform the addition.
Step3: If the FC is generated (i.e.1), then the result is +ve & in its
true form. If FC is not produced (i.e.0), then the result is –ve & in
its 2’s complement form.
Note: 2’s complement of B is (-B).
Note: we neglect the FC in 2’s complement method to avoid
overflow.
17
Subtraction with 2’s Complement
To substract smaller no. with larger no. method is as follows:
(1) Determine the 2’s Complement of smaller no.
(2) Add this to larger no.
(3) Omit the Carry (there is always a carry in this case)
Q.) Using direct & 2’s complement method, Subtract (1010)2
from (1111)2
Direct Method 2’s Complement Method
1111 1111
-1010 0110 (2’s complement)
0101 Carry 10101
Carry is Discarded.
Thus ans. is (0101)2
18
Subtraction with 2’s Complement
To subtract larger no. with smaller no. method is as follows:
(1) Determine the 2’s Complement of larger no.
(2) Add the 2’s complement to the smaller no.
(3) There is no Carry. The result is in 2’s complement form and is –
ve.
(4) To get an ans in true form, take the 2’s complement and change
the sign.
Q.) Using direct & 2’s complement method, Subtract (1010)2
from (1000)2
Direct Method 2’s Complement Method
1000 1000
-1010 0110 (2’s complement)
0010 No Carry 1110
No Carry is obtained.
Thus the difference is –ve & the true ans is the
2’s complement of (1110)2 i.e (0010)2
19
Addition in the 2’s Complement System
Since the 2’s complement form for representing signed
numbers is the most widely used in computer and
microprocessor-based systems.
Cases:
1. Both numbers positive
+7 + 4
2. Positive number with magnitude larger than negative number
+15 + (-6)
3. Negative number with magnitude larger than positive number
+16 + (-24)
4. Both numbers negative
-5 + (-9)
20
Addition in the 2’s complement System
Case 1: Both numbers positive
Lets Say +29 and +19
+29 0 001 1101 (Augend or A)
+19 0 001 0011 (Addend or B)
0 011 0000 (Sum=48)
Sign bit
The Sign bit of A & B are 0 & the sign bit of the sum is 0,
indicating that when the sum is positive they have the same
no. of bits.
21
Addition in the 2’s complement System
Case 2: Positive number with magnitude larger than negative
number
Lets Say +39 and -22
Note: -22 will be in 2’s complement form. As +22 [0 001 0110] must be
converted to -22 [1 110 1010]
+39 0 010 0111 (Augend or A)
-22 1 110 1010 (Addend or B)
1 0 001 0001 (Result=17)
Carry Sign bit
In this case:
Sign bit also participate in the addition.
Carry is always omitted and hence the result is 0001 0001 (=+17).
22
Addition in the 2’s complement System
Case 3: Negative number with magnitude larger than positive
number
Lets Say -47 and +29
-47 1 101 0001 (Augend or A)
+29 0 001 1101 (Addend or B)
1 110 1110 (Result=-18)
Sign bit
In this case
Sign bit of 1 in result indicate –ve no. means it is in 2’s complement
form i.e. last 7 bits 1101110 represents 2’s complement of the sum.
Thus the true magnitude of the sum can be found by taking 2’s
complement of 110 1110; the result is 10010(+18). Thus 1 110 1110
represents -18.
23
Addition in the 2’s complement System
Case 4: Both numbers negative
Lets Say -32 and -44
-32 1 110 0000 (Augend or A)
-44 1 101 0100 (Addend or B)
1 1 011 0100 (Result=-76)
Carry Sign bit
In this case
Carry is discarded and hence the result is 1011 0100.
The true magnitude of the sum is the 2’s complement of 011 0100, i.e 1
1001100 (-76).
Thus 2’s complement addition work in every case.
For n=8, If the decimal sum is outside -128 to +127 range then we get an
Overflow. 24
Overflow
If we add two 4 bits no. then sum should be 4 bit no. only if the sum
is greater than 4 bit no. then Overflow occurs.
Example: Lets (1001) + (1100)
For 4 variables range of 2’s comp. is -8 to +7
1001
1100
10101
Overflow
Trick to check or Condition for overflow:
x’y’z + xyz’ =0 (no overflow)
=1 (overflow)
Where x, y, and z are the sign bits of two no.’s, and result (z)
respectively. 25
Arithmetic Operations with Signed Numbers Cont.
Q: Perform each of the following subtractions of signed
numbers:
11100111 – 00010011
10001000 – 11100010
26
Hexa-decimal Arithmetic
Q: Perform the following operations:
2B16 + 8416
DF16 + AC16
C316 - 0B16
27
Codes
Codes: When the no. or letters are represented by specific
group of symbols we say that the no. or letters are encoded & the
group of symbols are called codes.
28
Coding
Coding is the process of altering the characteristics of information
to make it more suitable for intended application.
Coding schemes depend on:
Security requirements
Complexity of the medium of transmission
Levels of error tolerated
Need for standardization
Decoding
Decoding is the process of reconstructing source information from the
received encoded information
Decoding can be more complex than coding if there is no prior
knowledge of coding schemes
29
Classification of Codes
30
Self Complementing or Reflective Code
Code of 9 is complement of 0.
Code of 8 is complement of 1.
Code of 7 is complement of 2.
……..
Example: 2421 Code
31
Sequential Code
Each succeeding Code is 1 binary no. greater than the preceding
code. Example: 8421, XS-3 Code.
Alphanumeric Code
Can represent no., character, symbols, and small instructions.
Example: ASCII
32
How do we select a coding scheme?
It should have some desirable properties
ease of coding
ease in arithmetic operations
minimum use of hardware
error detection property
ability to prevent wrong output during transitions
33
Binary Coded Decimal (BCD)
Binary coded decimal (BCD) is a way to express each of the
decimal digits with a binary code.
The 8421 Code
The 8421 is a type of BCD code.
Binary coded decimal means that each decimal digit, 0 through
9, is represented by a binary code of four bits
The designation 8421 indicates the binary weights of the four
bits (23, 22, 21, 20 )
34
The 8421 BCD Code
You should realize that, with four bits, sixteen numbers
(0000 through 1111) can be represented but that, in the 8421
code, only ten of these are used.
The six code combinations that are not used – 1010, 1011,
1100, 1101, 1110, 1111 – are invalid in the 8421 BCD code
Convert the following decimal numbers to BCD
15 00010101
98 10011000
2469 0010010001101001
Convert the following BCD code to decimal numbers
10000110 86
001101010001 351
35
BCD Addition
Step 1: Add the two BCD numbers, using the rules for
binary addition.
Step 2: If a 4-bit sum is equal to or less than 9, it is a
valid BCD number
Step 3: If a 4-bit sum is greater than 9, or if a carry out of the
4-bit group is generated, it is an invalid result. Add 6
(0110) to the 4-bit sum in order to skip the six invalid states
and return the code to 8421. If a carry results when 6 is
added, simply add the carry to the next 4-bit group
Add the following BCD Numbers
00010110 + 00010101
01100111 + 01010011 36
Introduction to the Gray Code
The Gray code is unweighted and is not an arithmetic code;
The important feature of the Gray code is that it exhibits only a single
bit change from one code word to the next in sequence.
Also known as Reflected Binary Code or Cyclic Code.
Unit distance Code & Minimum error Code.
This property is very important in many applications for error detection
Frank Gray
37
The Gray Code Contd.
Two successive values
differ in only 1 bit.
Switching is reduced in
GC.
38
Binary-to-Gray Code Conversion
Binary is converted to GC to reduce the switching operation.
Step1: Record the MSB as it is.
Step2: Add the MSB to the next adjacent bit, record the sum &
neglect the carry.
Step3: Repeat the process.
Convert the binary number to Gray code
11000110 Ans: 10100101
39
Binary-to-Gray Code Conversion
Binary is converted to GC to reduce the switching operation.
Step1: Record the MSB as it is.
Step2: Add the MSB to the next bit, record the sum & neglect the carry. Or
perform X-OR operation.
Step3: Repeat the process.
Note: X-OR is odd 1’s detector. (AB’+A’B)
Convert the binary number to Gray code
11000110 Ans: 10100101 40
Gray-to-Binary Conversion
Gray to binary or binary to gray MSB remains the same.
The most significant bit (left-most) in the binary code is the same
as the corresponding MSB in the Gray code
Add each binary code bit generated to the Gray code bit in the
next adjacent position. Discard carries.
Generally:
b (1)= g (1)
b (2)= b (1) X-OR g (2)
b (3)= b (2) X-OR g (3)
b (4)= b (3) X-OR g (4)
b (5)= b (4) X-OR g (5)
Convert the binary number to Gray code
10101111 Ans: 11001010
41
End of the Class
42