0% found this document useful (0 votes)
2 views

04 Week02 1&2Complement

The document discusses the concepts of complements in number systems, specifically focusing on Diminished Radix Complement and Radix Complement for subtraction. It explains how to perform subtraction using these complements, including examples in both decimal and binary systems. Additionally, it covers signed integers and the arithmetic operations of addition and subtraction using signed-magnitude and signed-complement methods.

Uploaded by

HARSH
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

04 Week02 1&2Complement

The document discusses the concepts of complements in number systems, specifically focusing on Diminished Radix Complement and Radix Complement for subtraction. It explains how to perform subtraction using these complements, including examples in both decimal and binary systems. Additionally, it covers signed integers and the arithmetic operations of addition and subtraction using signed-magnitude and signed-complement methods.

Uploaded by

HARSH
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 25

1’s and 2’s

Complement
Complements
• Subtraction of numbers requires a different algorithm
than addition.
• Adding a complement of a number is equivalent to
subtraction.
• We will discuss two complements:
Diminished Radix Complement
Radix Complement
• Subtraction will be accomplished by adding a
complement.
Diminished Radix Complement
Given a number N in Base r having n digits, the (r-1)'s
complement (called the Diminished Radix Complement) is
defined as:
(rn - 1) - N
Example:
For r = 10, N = 123410, n = 4 (4 digits),we have:
(rn - 1) = 10,000 -1 = 999910
The 9's complement of 123410 is then:
999910 - 123410 = 876510
Binary 1's Complement
For r = 2, N = 01110011 2, n = 8 (8 digits), we have:
(rn - 1) = 256 -1 = 25510 or 111111112
The 1's complement of 011100112 is then:
11111111 2
- 01110011 2
----------------
100011002
NOTE: Since the 2n-1 factor consists of all 1's and
since 1 - 0= 1 and 1 - 1 = 0, forming the one's
complement consists of complementing each
individual bit.
Radix Complement
Given a number N in Base r having n digits, the r's
complement (called the Radix Complement) is defined as:
rn - N for N 0 and
0 for N = 0
Note that the Radix Complement is obtained by adding 1
to the Diminished Radix Complement.

Example:
For r = 10, N = 123410, n = 4 (4 digits), we have:
rn = 10,00010
The 10's complement of 123410 is then
10,00010 - 123410 = 876610 or 8765 + 1 (9's complement plus 1)
Binary 2's Complement
For r = 2, N = 011100112, n = 8 (8 digits), we have:

(rn ) = 25610 or 1000000002

The 2's complement of 011100112 is then:


1000000002
- 01110011 2
----------------
100011012

Note that this is the 1's complement plus 1.


Binary 2's Complement
Examples
100000000
- 11011100
-----------------
00100100
100000000 (The 2's complement of 0 is zero!)
- 00000000
-----------------
00000000
100000000
- 11111111  (could this be -1)?
-----------------
00000001  (could this be +1?)
Efficient 2’s Complement
Given: an n-bit binary number:
an-1an-2 ai+110...00

Where for some digit position i, ai is 1 and all digits to the


right are 0, form the twos complement value this way:
Leave ai equal to 1 (unchanged),
and
Leave rightmost digits 0 (unchanged),
and
` complement all other digits to the left of ai.
(0 replaces 1, 1 replaces 0)
Two's Complement
Example
01101011100011100000
First 1 from right --- --

Complement leftmost digits


------------------
10010100011100100000
------ Leave these

This  0110100111100
becomes 1001011000100

This  1000000000000
becomes 1000000000000
Subtraction with Radix
Complements
Subtract two n-digit, unsigned numbers M  N,
in base r as follows:
1. Add the minuend M to the r's complement of the
subtrahend N to perform:
M + (r - N) = M  N + r
n n

2. If M  N, the sum will produce an end carry, rn which is


discarded; what is left is the result, M  N.
3. If M < N, the sum does not produce an end carry and is
equal to rn - ( N - M ), which is the r's complement of ( N 
M ). To obtain the answer in a familiar form, take the r's
complement of the sum and place a negative sign in front.
Example: Find 543 – 123
10 10
1). Form 10's complement of 123: 1000
- 123
-----------
877

2). Add the two: 543


(+) 877
-------------
1420
3). Since M > N, we discard the carry. Ans: 420
Example: Find 123 - 543
10 10

1). Form 10's complement of 543: 1000


 543
-----------
457
2). Add the two: 123
(+) 457
-------------
3). Since M < N, form complement 580 (no carry)
1000
( ) 580
-----------
Answer is ( )420. 420
Binary Example
Compute: 1010100 - 1000011
1). Form 2's complement of 1000011: 1000011
0111101

1010100
2). Add the two: 0111101
(+) -------------
1 0010001 (a carry )

3). Since M  N, discard the carry. Ans. = 0010001


Another Binary Example
Compute: 1000011 - 1010100
1010100
1). Form 2's complement of 1010100: 0101100

2). Add the two: 1000011


0101100
(+) -------------
1101111 (no carry)
3). Since M < N, complement the result.
Ans. = ( ) 0010001
Subtract: Add 1’s Complement
We can use addition of the 1's complement to subtract two
numbers with a minor modification.

Since (r-1)'s complement is one less than the r's


complement, the result produces a sum which is one less
than the correct sum when an end carry occurs.

We can simply add in the end carry when it occurs to


correct the answer.

If the end carry does not occur, the result is negative and
we can use the 1's complement to represent the negative
result.
1’s Complement Subtraction
Use 1's complement to compute 1010100 - 1000011
1). Form 1's complement of 1000011: 1000011
0111100
2). Add the two: 1010100
0111100
(+) -------------
10010000 (a carry)
3). Add carry, end around (+) 0000001
-------------
0010001
Ans. = 0010001
1’s Complement Subtraction
Use 1's complement for computing 1000011 - 1010100
1). Form 1's complement of 1010100: 1010100
0101011
1000011
2). Add the two:
0101011
(+) -------------
1101110 (no carry)

3). Form 1's complement: 0010001


4). The answer has a negative sign.
Ans. = ( ) 0010001
Signed Integers
Positive numbers and zero can be represented by unsigned n-
digit, radix r numbers. We need a representation for
negative numbers.
To represent a sign (+ or -) we need exactly one more bit of
1
information (1 binary digit gives 2 = 2 elements which is
exactly what is needed).
Since most computers use binary numbers, by convention,
(and for convenience), the most significant bit is interpreted
as a sign bit as shown below:
san-2  a2a1a0
Where: s = 0 for Positive numbers
s = 1 for Negative numbers
and ai are 0 or 1
Interpreting the Other Digits
Given n binary digits, the digit with weight 2(n-1) is the sign and the
digits with weights 2(n-2) down to 2(0) can be used to represent 2(n-
1) distinct elements.
There are several ways to interpret the other digits. Here are three
popular choices:
1. Signed-Magnitude -- here the n-1 digits are interpreted as a
positive magnitude.
2. Signed-Complement -- here the digits are interpreted as the rest of
the complement of the number. There are two possibilities here:
2a. Signed One's Complement --
(use the 1's Complement to compute)
2b. Signed Two's Complement --
(use the 2's Complement to compute)
Example: Given r=2, n=3
We have the following interpretations for signed integer
representation:
Number Sign-Mag. 1's Comp. 2's Comp.
+3 011 011 011
+2 010 010 010
+1 001 001 001
+0 000 000 000
-0 100 111 ---
-1 101 110 111
-2 110 101 110
-3 111 100 101
-4 --- --- 100
Addition with Signed Numbers
Caution: If you use all rn possible combinations of n
radix r digits, some operations on elements of the set
will produce elements which will not be represented in
the set.

Example: Add unsigned, 3-bit integers 1012 to 1002 to


get 10012 (5 + 4 = 9). This result cannot be represented
in the set of 3-bit unsigned integers. An overflow is said
to have occurred.
Signed-Magnitude Arithmetic
Addition:
If signs are the same:
1. Add the magnitudes.
2. Check for overflow (a carry into the sign bit).
3. The sign of the result is the same.
If the signs differ:
1. Subtract the magnitude of the smaller from the magnitude of
the larger.
2. Use the sign of the larger magnitude for the sign of the result.
3. Overflow will never occur.
Subtraction:
Complement the sign bit of the number you are subtracting and
follow the rules for addition.
Sign-Magnitude Examples
Same signs
000 + 001 = 001 (signs are the same)
010 + 010 = x00 (Overflow into sign bit)
101 + 101 = 110 (signs are the same)
110 + 110 = x00 (Overflow into sign bit)
Different signs
001 + 110 = 101 (010  001, take  sign)
111 + 010 = 101 (011  001, take  sign)
101 + 010 = 001 (010  001, take + sign)
100 + 000 = ?00 (is it + or - zero?)
Signed-Complement
Arithmetic
Addition:
1. Add the numbers including the sign bits, discarding a
carry out of the sign bits (2's Complement), or using
an end-around carry (1's Complement).
2. If the sign bits were the same for both numbers and
the sign of the result is different, an overflow has
occurred.
3. The sign of the result is computed in step 1.
Subtraction:
Form the complement of the number you are subtracting
and follow the rules for addition.
Binary
Adder/Subtractor
Subtraction can be accomplished by addition of the Two's
Complement. Thus we::
1. Complement each bit (One's Comp.)
2. Add one to the result.
The following circuit computes A - B:
It computes A - B when A(3) A(2) A(1) A(0)
B(3) B(2) B(1) B(0)
the Carry-In is "1" by C(0)

forming the One's


Complement of B using
x y x y x y x y
EXOR gates and adding C(4) Co
FA
Ci
C(3)
Co
FA
Ci
C(2)
Co
FA
Ci
C(1)
Co
FA
Ci

one S S S S

S(3) S(2) S(1) S(0)

You might also like