Topic 2 - 3 Addition Subtraction
Topic 2 - 3 Addition Subtraction
p
Fundamentals of Computer Science
1 + 1 iis 0 with
i h a carry
Carry Values
111111
1010111
+11 0 0 1 0 1 1
10100010
2
14
Exercise 2.3.1
Solve these problems in binary:
1 9810 + 5410
1.
2. 710 + 410
3. 3410 + 1010
4. 258 + 178
5. 1238 + 4568
6. 678 + 258
7. ABC16 + DEF16
Subtracting Binary Numbers
11
2222
1010111
- 111011
0011100
4
15
E
Exercise
i 2 2.3.2
32
Solve these problems in binary:
1. 6310 – 2910
2. 9810 - 3410
3. 11710 - 7010
4. 538 - 148
5. 1268 - 748
6. FAD16 - BEC16
Signed Integer Representation
2.4 Signed Integer Representation
7
2.4 Signed Integer Representation
y In
I an 8-bit
8 bi word,
d signed
i d magnitude
i d
representation places the absolute value of
the number in the 7 bits to the right of the sign
bit.
8
2.4 Signed Integer Representation
y For example,
example in 8-bit signed magnitude
magnitude,
positive 3 is: 00000011
y Negative
g 3 is: 10000011
y Computers perform arithmetic operations on
signed magnitude numbers in much the same
way as humans carry out pencil and paper
arithmetic.
y Humans often ignore the signs of the operands while
performing a calculation, applying the appropriate sign
after the calculation is complete.
9
2.4 Signed Integer Representation
y Example:
y Using signed magnitude binary
arithmetic, find the sum of 75 and
46.
y First, convert 75 and 46 to
binary, and arrange as a sum
binary sum,
but separate the (positive) sign
bits from the magnitude bits.
10
2.4 Signed Integer Representation
y Example:
y Using signed magnitude binary
arithmetic, find the sum of 75 and
46.
y Just as in decimal arithmetic,
we find the sum starting with
the rightmost bit and work left.
11
2.4 Signed Integer Representation
y Example:
y Using signed magnitude binary
arithmetic, find the sum of 75 and
46.
y In the second bit, we have a
carry, so we note it above the
carry
third bit.
12
2.4 Signed Integer Representation
y Example:
y Using signed magnitude binary
arithmetic, find the sum of 75 and
46.
y The third and fourth bits also
give us carries
carries.
13
2.4 Signed Integer Representation
y Example:
y Using signed magnitude binary
arithmetic, find the sum of 75 and
46.
y Once we have worked our way
through all eight bits,
bits we are
done.
IIn thi
this example,
l we were careful
f l careful
f l to
t pick
i k two
t
values whose sum would fit into seven bits. If that is not
the case, we have a problem.
14
2.4 Signed Integer Representation
y Example:
y Using signed magnitude binary
arithmetic, find the sum of 107 and
46.
y We see that the carry from the
seventh bit overflows and is
discarded, giving us the
erroneous result: 107 + 46 = 25.
15
2.4 Signed Integer Representation
16
2.4 Signed Integer Representation
17
2.4 Signed Integer Representation
y For example,
example in 8-bit one
one’s
s complement
complement,
positive 3 is:00000011
y Negative
g 3 is: 11111100
y In one’s complement, as with signed
magnitude, negative values are indicated by a 1
in the high order bit.
y Complement systems are useful because they
eliminate the need for subtraction
subtraction. The
difference of two values is found by adding the
minuend to the complement
p of the subtrahend.
18
2.4 Signed Integer Representation
y With one
one’s
s complement
addition, the carry bit is
“carried around” and added
to the sum.
y Example: Using one’s
complement binary arithmetic
arithmetic, find
the sum of 48 and - 19
We note
W t that
th t 19 in
i one’s
’ complement
l t is
i 00010011,
00010011 so -19
19
in one’s complement is: 11101100.
19
2.4 Signed Integer Representation
20
2.4 Signed Integer Representation
21
2.4 Signed Integer Representation
y With two’s
two s complement arithmetic
arithmetic, all we do is add
our two binary numbers. Just discard any carries
emitting from the high order bit.
– Example: Using one’s
complement binary
arithmetic, find the sum of
48 and - 19.
We note that 19 in one’s complement is: 00010011, so -
19 in one’s complement is: 11101100,
and -19 in two’s complement
p is: 11101101.
22
2.4 Signed Integer Representation
23
2.4 Signed Integer Representation
y Example:
y Using two’s complement binary
arithmetic, find the sum of 107 and 46.
y We see that the nonzero carry
from the seventh bit overflows into
the sign bit
bit, giving us the
erroneous result: 107 + 46 = -103.
25
2.4 Signed Integer Representation
26
2.4 Signed Integer Representation
27
Bi
Binary S
Subtraction
bt ti
y The most common wayy of subtractingg binaryy numbers is
done by first taking the second value (the number to be
subtracted) and apply what is known as two's
complement,
l this
h is ddone in two steps:
y complement each digit in turn (change 1 for 0 and 0 for 1).
y add 1 (one) to the result.
result
note: the first step by itself is known as one's complement.
Bi
Binary S
Subtraction
bt ti
| Byy applying
pp y g these steps
p you
y are effectivelyy turningg the
value into a negative number, and as when dealing with
decimal numbers if you add a negative number to a
ppositive number then you
y are effectivelyy subtractingg to
the same value.
In other words 25 + ((-8)) = 17, which is the same as
writing 25 - 8 = 17.
note: When subtracting binary values it is important to maintain the
same amount off digits
d for
f eachh number,
number
b even iff it means placing
l
zeroes to the left of the value to make up the digits, for instance in
our example we have added a zero to the left of the value 1100110 to
make the amount of numerals up to 8 (one byte) 01100110.
01100110
Bi
Binary S
Subtraction
bt ti
y An example,
p let's do the followingg subtraction
11101011 - 01100110 (23510 - 10210)
Exercise
Convert these decimal numbers to binaryy usingg Two’s
Complement:
1. -8
2. -3
3. -15
4. -34
5. -128
Exercise
Solve the following arithmetic operation using 2’s
complement (8 bit method)
- 1510 - 3010
4110 - 1210
15010 – 4910
100111102 – 010110012