Binary Arithmetic: - Binary Addition - Binary Subtraction - Binary Multiplication - Binary Division
Binary Arithmetic: - Binary Addition - Binary Subtraction - Binary Multiplication - Binary Division
• Binary addition
• Binary subtraction
• Binary multiplication
• Binary division
1
Complements of Binary
Numbers
• 1’s complements
• 2’s complements
2
Complements of Binary
Numbers
• 1’s complement
• Change all 1s to 0s and all 0s to 1s
1 0 1 0 1 0 1 0
0 1 0 1 0 1 0 1
3
Complements of Binary
Numbers
• 2’s complement
• Find 1’s complement and then add 1
1 0 1 0 1 0 1 0
1
1’s complement 0 1 0 1 0 1 0 1
Input bits
Adder Carry
Output bits (sum) In (add 1)
2’s complement 0 1 0 1 0 1 1 0
4
Signed Numbers
5
Topics for Signed Numbers
• Signed-magnitude form
• 1’s and 2’s complement form
• Decimal value of signed numbers
(How to convert)
• Range of values (max and min)
• Floating-point numbers
6
Signed Numbers
• Signed-magnitude form
– The sign bit is the left-most bit in a signed
binary number
– A 0 sign bit indicates a positive magnitude
– A 1 sign bit indicates a negative magnitude
7
Signed Numbers
• 1’s complement form
– A negative value is the 1’s complement of
the corresponding positive value
• 2’s complement form
– A negative value is the 2’s complement of
the corresponding positive value
8
Signed Numbers
9
Signed Numbers
• Range of Values
Total combinations = 2n
2’s complement form:
– (2n – 1) to + (2n – 1 – 1)
11111111 = -1
10000001 = -127
01111111 = +127 11
Signed Numbers
• Floating-point numbers
– Can represent very large or very small numbers
based on scientific notation. Binary point “floats”.
• Two Parts
– Mantissa represents magnitude of number
– Exponent represents number of places that
binary point is to be moved
• Three forms
– Single-precision (32 bits) float
– Double-precision (64 bits) double
– Extended-precision (80 bits) long double
– Also have Quadruple and Quadruple extended!
12
Single Precision
32 bits
15
Examples
Type Exponent Mantissa Value
Zero 0000 0000 000 0000 0000 0000 0000 0000 0.0
One 0111 1111 000 0000 0000 0000 0000 0000 1.0
-39
Denormalized 0000 0000 100 0000 0000 0000 0000 0000 5.9×10
number
38
Large normalized 1111 1110 111 1111 1111 1111 1111 1111 3.4×10
number
-38
Small normalized 0000 0001 000 0000 0000 0000 0000 0000 1.18×10
number
Infinity 1111 1111 000 0000 0000 0000 0000 0000 Infinity
NaN 1111 1111 010 0000 0000 0000 0000 0000 NaN
16
Double Precision
17
Arithmetic Operations with
Signed Numbers
• Addition
• Subtraction
• Multiplication
• Division
18
Arithmetic Operations with
Signed Numbers
Addition of Signed Numbers
• The parts of an addition function are:
– Augend - The first number
– Addend - The second number
– Sum - The result
Numbers are always added two at a time.
19
Arithmetic Operations with
Signed Numbers
Four conditions for adding numbers:
1. Both numbers are positive.
2. A positive number that is larger than a
negative number.
3. A negative number that is larger than
a positive number.
4. Both numbers are negative.
20
Arithmetic Operations with
Signed Numbers
Signs for Addition
• When both numbers are positive, the
sum is positive.
• When the larger number is positive and
the smaller is negative, the sum is
positive. The carry is discarded.
21
Arithmetic Operations with
Signed Numbers
Signs for Addition
• When the larger number is negative and
the smaller is positive, the sum is
negative (2’s complement form).
• When both numbers are negative, the
sum is negative (2’s complement form).
The carry bit is discarded.
22
Examples (8 bit numbers)
• Add 7 and 4 (both positive) 00000111 7
+00000100 +4
00001011 11
Sign Incorrect
Magnitude Incorrect
24
Arithmetic Operations with
Signed Numbers
Subtraction of Signed Numbers
• The parts of a subtraction function are:
– Minuend - The first number
– Subtrahend - The second number
– Difference - The result
Subtraction is addition with the sign of the
subtrahend changed.
25
Arithmetic Operations with
Signed Numbers
Subtraction
• The sign of a positive or negative binary
number is changed by taking its 2’s
complement
• To subtract two signed numbers, take
the 2’s complement of the subtrahend
and add. Discard any final carry bit.
26
Subtraction Examples
• Find 8 minus 3. 00001000 8 Minuend
+11111101 - 3 Subtrahend
Discard carry 1 00000101 5 Difference
28
Arithmetic Operations with
Signed Numbers
There are two methods for multiplication:
• Direct addition
– add multiplicand multiple times equal to the
multiplier
– Can take a long time if multiplier is large
• Partial products
– Similar to long hand multiplication
The method of partial products is the most
commonly used.
29
Arithmetic Operations with
Signed Numbers
Multiplication of Signed Numbers
• If the signs are the same, the product is
positive. (+ X + = + or - X - = +)
• If the signs are different, the product is
negative. (+ X - = - or - X + = -)
30
Multiplication Example
• Both numbers must be in uncomplemented form
• Multiply 3 by -5.
Opposite signs, so product will be negative.
310 = 000000112 00000011 Multiplicand
-510 = 111110112 X 00000101 Multiplier
00000011 First partial product
+ 0000000 Second partial product
2’s complement of -5 00000011 Sum of 1st and 2nd
00000101 + 000011 Third partial product
00001111 Sum and Final Product
32
Arithmetic Operations with
Signed Numbers
Division of Signed Numbers
• If the signs are the same, the quotient is
positive. (+ ÷ + = + or - ÷ - = +)
• If the signs are different, the quotient is
negative. (+ ÷ - = - or - ÷ + = -)
33
Division Example
• Both numbers must be in uncomplemented form
• Divide 01100100 by 00110010.
Both numbers are positive so
quotient will be positive.
Set the quotient to zero initially. quotient: 00000000
01100100 Dividend
Subtract the divisor from the + 11001110 2’s complement of Divisor
dividend by using 2’s complement 1 00110010 First partial remainder
addition. (11001110)
Ignore the carry bit. Add 1 to quotient: 00000000 + 1 = 00000001
00110010 First partial remainder
Subtract the divisor from the + 11001110 2’s complement of Divisor
1st partial remainder using 2’s 1 00000000 zero remainder
complement addition.
Add 1 to quotient: 00000001 + 1 = 00000010
34
So final quotient is 00000010 and final remainder is 00000000
Hexadecimal Numbers
35
Hexadecimal Numbers
• Decimal, binary, and hexadecimal
numbers
• 4 bits is a nibble
• FF16 = 25510
36
Hexadecimal Numbers
• Binary-to-hexadecimal conversion
• Hexadecimal-to-decimal conversion
• Decimal-to-hexadecimal conversion
37
Hexadecimal Numbers
• Binary-to-hexadecimal conversion
1. Break the binary number into 4-bit
groups
2. Replace each group with the
hexadecimal equivalent
• Convert 1100101001010111 to Hex
C A 5 7 = CA5716
• Convert 10A416 to binary
• Hexadecimal-to-decimal conversion
1. Convert the hexadecimal to groups of 4-bit
binary
2. Convert the binary to decimal
39