33. r’s and r-1’s COMPLEMENTS
USED FOR
SIMPLIFY SUBTRACTION OPERATIONS
TWO TYPES
Decimal Binary Octal Hexadecimal
r’ s complement 10’s 2’s 8’s 16’s
r - 1’s complement 9’s 1’s 7’s 15’s
Here r is base or radix of number system
r’s Complement
LEAVE ALL LSD ZEROS UNCHANGED
SUBSTRACT FIRST NONZERO LSD FROM r
SUBSTRACT ALL OTHER HIGHER DIGITS FROM r-1
r-1’s Complement
SUBSTRACT EVERY DIGIT FROM r-1
Note: r’s Complement=r-1’s Complement +1
34. 34
RULES TO GET COMPLEMENTS
9’S
SUBSTRACT EVERY DIGIT FROM 9
Example: 9’s complement of 134795 is 865204
10’s=(9’s complement +1)
10’s complement can be Directly calculated by following steps:
LEAVE ALL LSD ZEROS UNCHANGED
SUBSTRACT FIRST NONZERO LSD FROM 10
SUBSTRACT ALL OTHER HIGHER DIGITS FROM 9
Example:10’s complement of 134795 is 865205
Example: find the 9’s and 10’s complements of 314700.
2’S
LEAVE ALL LS ZEROS AND FIRST NON ZERO DIGIT UNCHANGED
REPLACE 1 BY 0 AND 0 BY 1 IN ALL HIGHER DIGITS
1’S
1 IS CHANGED TO 0 AND 0 IS CHANGED TO 1
Note: COMPLEMENT OF A COMPLEMENT RESTORES THE ORIGINAL VALUE OF THE NUMBER
36. Subtraction using r’s complement:
To find M-N in base r, we add M + r’s complement of N
1) If M > N then result is M – N + c (c is an end around carry and
Just neglect it.
2) If M < N then result is rn
–(N-M) which is the r’s complement
of the result. (to get actual number take r’s complement again and
put – sign before number)
08/23/1536
38. Subtraction using r-1’s complement
The same rules apply to subtraction using the (r-1)’s complements.
The only difference is that when an end carry is generated, it is not
discarded but added to the least significant digit of the result.
Also, if no end carry is generated, then the answer is negative and
in the (r-1)’s complement form.
08/23/1538
59. Practice problems
1. Given the binary number 11011011, what is its decimal value if it
is a
a. unsigned binary number
b. Signed binary number
c. 2's complement number
2. Convert +24 into a 2's complement number
08/23/1559
Ans: 219
Ans: -37
Ans: -37
Ans: 011000 Note the leading zero!
60. 3. Convert -24 into a 2's complement number
4. Convert -1 into a 2's complement number (assume a 4-bit result)
5. What is the minimum (maximum negative) number which can be
represented using a 6-bit 2's Complement representation? What is the
maximum (positive) number?
08/23/1560
Ans: 1111
Ans: 101000
Ans: +31, -32
Practice problems (Cont…)
61. Practice problems (Cont…)
Convert, if possible, the following decimal numbers to 2's
complement assuming an 8-bit binary representation for all.
i. 1 = ii. -1 =
iii. 72 = iv. 127 =
v. -127 = 10000001 vi. -105 = 10010111
vii. 255 = viii. -255 =
ix. -6 =
08/23/1561
00000001 11111111
01001000 01111111
Not Possible
11111010
Not Possible
62. Practice problems (Cont…)
Convert the following 2's complement numbers to decimal.
i. 1111 = ii. 11110000 =
iii. 001101 = iv. 11111111 = -1
v. 0000000 = 0 vi. 01 = 1
08/23/1562
-1 -16
13
65. As a general rule, it is a good idea to avoid the use operators
that cause side effects inside of compound expressions. This
includes all assignment operators, plus the increment and
decrement operators.
08/23/1565
Note: As a general rule, it is a good idea to avoid the use of operators that
alter the value of same variable more than one in the same expression,
because this causes side effects. This includes all assignment operators, plus
the increment and decrement operators.
For all side effects result will be compiler dependent.
67. #include<stdio.h>
void main() {
int i=3;
printf("%d",++i + ++i); o/p 9 for turbo c
10 by codepad
}
The results are undefined. You're modifying a variable more than once in an expression
(or sequence point to be more accurate).
Modifying a variable more than once between sequence points is undefined, so don't do
it.
08/23/1567