0% found this document useful (0 votes)
8 views32 pages

Topic 2 - 3 Addition Subtraction

addition subtraction

Uploaded by

2024235138
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views32 pages

Topic 2 - 3 Addition Subtraction

addition subtraction

Uploaded by

2024235138
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

CSC401

p
Fundamentals of Computer Science

Topic 2: Addition and Subtraction


1
Arithmetic in Binary

Remember that there are only 2 digits in binary, 0 and 1

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

Remember borrowing? Apply that concept here:

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

y The conversions we have so far presented have


involved only positive numbers.
y To represent
p negative
g values,, computer
p systems
y
allocate the high-order bit to indicate the sign of a
value.
y The
Th hhigh-order
h d bit b is the
h leftmost
l f bit
b in a bbyte. It is also
l called
ll d the
h
most significant bit.
y The remaining bits contain the value of the number
number.

7
2.4 Signed Integer Representation

y There are three ways in which signed binary


numbers may be expressed:
y Signed magnitude,
y One’s complement and
y Two’s complement.

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

y Signed magnitude representation is easy for


people to understand, but it requires
complicated computer hardware.
y Another disadvantage of signed magnitude is
that it allows two different representations for
zero: positive zero and negative zero.
y For these reasons (among others) computers
systems employ complement systems for
f
numeric value representation.

16
2.4 Signed Integer Representation

y In diminished radix complement systems,


systems a
negative value is given by the difference
between the absolute value of a number
and one less than its base.
y In the binary system
system, this gives us one
one’s
s
complement. It amounts to little more than
flipping
pp g the bits of a binary
y number.

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

y Although the “end


end carry around”
around adds some
complexity, one’s complement is simpler to
implement than signed magnitude.
y But it still has the disadvantage of having two
different representations for zero: positive
zero and negative zero.
y Two’s complement solves this problem.It is
the radix complement off the binary numbering
system.

20
2.4 Signed Integer Representation

y To express a value in two’s


two s complement:
y If the number is positive, just convert it to binary and you’re
done.
y If the number is negative, find the one’s complement of the
number and then add 1.
y Example:
y In 8-bit one’s complement, positive 3 is: 00000011
y Negative
g 3 in one’s complement
p is: 11111100
y Adding 1 gives us -3 in two’s complement form: 11111101.

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

y When we use any finite number of bits to


represent a number, we always run the risk of
the result of our calculations becoming too large
to be stored in the computer.
y While we can’t always prevent overflow, we can
always detect overflow.
y In complement arithmetic, an overflow condition
i easy tto detect.
is d t t

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.

Rule for detecting signed two’s complement overflow: When


the “carry in” and the “carry out” of the sign bit differ,
overflow
fl has
h occurred. d
24
2.4 Signed Integer Representation

y Signed and unsigned numbers are both useful


useful.
y For example, memory addresses are always unsigned.

y Using the same number of bits


bits, unsigned integers
can express twice as many values as signed
numbers.
y Trouble arises if an unsigned value “wraps around.”
y In four bits: 1111 + 1 = 0000.
y Good programmers stay alert for this kind of
problem.

25
2.4 Signed Integer Representation

y Overflow and carry are tricky ideas


ideas.
y Signed number overflow means nothing in the
context of unsigned numbers
numbers, which set a carry
flag instead of an overflow flag.
y If a carry out of the leftmost bit occurs with an
unsigned number, overflow has occurred.
y Ca
Carry
yaand
dooverflow
e o occu
occur independently
depe de t y o
of eac
each
other.
The table on the next slide summarizes these ideas.

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy