Addition and Subtraction

Download Report

Transcript Addition and Subtraction

Addition and Subtraction
Outline
• Arithmetic Operations (Section 1.2)
– Addition
– Subtraction
– Multiplication
• Complements (Section 1.5)
– 1’s complement
– 2’s complement
• Signed Binary Numbers (Section 1.6)
– 2’s complement
– Addition
– Subtraction
Addition
• 101101+100111
• Rules
– Any carry obtained in a given significant
position is used by the pair of digits one
significant position higher
Subtraction
• 101101-100111
• The borrow in a given significant
position adds 2 to a minuend digit
Multiplication
• 1011 X 101
Complement
• 1’s complement
• 2’s complement
1’s complement
• Rule: 1’s complement of a binary
number is formed by changing
– 1’s to 0’s
– 0’s to 1’s
• 1011000→0100111
• 0101101 →
2’s Complement
• Alternative Method
– Write the 1’s complement
– Add 000…1 to 1’s complement
• Example
– 1101100
– 0010011 (1’s complement)
– 0010100 (2’s complement)
Unsigned Subtraction
• X-Y
– Determine Y’s 2’s complement
– X+(2’s complement of Y)
• If X is larger or equal to Y, an end carry
will result. Discard the end carry.
• If X is less than Y, no end carry will result.
To obtain the answer in a familiar form,
take the 2’s complement of the sum and
place a negative sign in front.
Subtraction of Unsigned Number
•
•
•
•
•
Example 1.7 (2’s complement)
X=1010100
Y=1000011
X-Y (Discard end carry)
Y-X (No end carry)
Signed Binary Number
• Signed-magnitude representation
• Signed 1’s complement representation
• Signed 2’s complement representation
Signed Magnitude
Representation
• Rules
– Represent the sign in the leftmost position
– 0 for positive
– 1 for negative
• Example
– 01001↔(+)9
– 11001↔(-)9
• Used in ordinary arithmetic, but not in
computer arithmetic
Interpretation
• The user determines whether the
number is signed or unsigned
– 01001
• 9 (unsigned binary)
• +9 (signed binary)
– 11001
• 25 (unsigned binary)
• -9 (signed binary)
Signed Complement System
• A signed complement system negates
a number by taking its complement
• Example
– 00001001 (9)
– 11110110 (-)9 in signed 1’s complement
– 11110111 (-)9 in signed 2’s complement
• Usage:
– 1’s complement: Seldom used
– 2’s complement: Most common
Arithmetic Addition
• The rule for adding signed numbers in 2’s
complement form is obtained from
addition of two numbers
• A carry out of the sign bit is discarded
• In order to obtain correct answer, we
must ensure that the result has a sufficient
number of bits to accommodate the sum
• Useful Facts
– Positive numbers have 0 in the leftmost bit
– Negative numbers have a 1 in the leftmost bit
Negative Number
• Determine the value of a negative
number in signed 2’s complement by
converting the number to a positive
number to place it in a more familiar
form
– 11111001 is negative because the left
most bit is 1.
– 2’s complement: 00000111 (+7)
– Therefore, 11111001 is -7