Вы находитесь на странице: 1из 5

Discrete Mathematics 2002 Lecture 6, 2-August-2002

Storing Integers in a 4-bit Computer


• Recall: 24 = 16 integers can be represented in 4
bits; a convenient range is –8 ≤ n ≤ 7
• The first bit is the sign bit, which is 0 if n is
positive or zero, & 1 if n is negative
• If n ≥ 0, the remaining 3 bits are the binary repn
of n (with leading zeros if necessary) – so the
computer repn is identical to the 4-bit binary repn
• If n < 0, the remaining 3 bits are the binary repn
of n + 8 – so the computer repn of n is the 4-bit
binary repn of n +16
1

Table of 4-bit Representations


Integer Repn Integer Repn
–8 1000 0 0000
–7 1001 1 0001
–6 1010 2 0010
–5 1011 3 0011
–4 1100 4 0100
–3 1101 5 0101
–2 1110 6 0110
–1 1111 7 0111 2

Adding Integers on a 4-bit Computer


• Sometimes two nos can’t be added on the 4-bit
computer – e.g. the result of 4 + 6 is too large to
be represented
• However, provided we look only at nos that can
be added, addition is carried out as usual for
binary nos, except that a 1 in the 5th column
from the right is ignored
• Example: Verify that the addition (–3) + 7 is
carried out correctly on a 4-bit computer
3

1
Discrete Mathematics 2002 Lecture 6, 2-August-2002

Why does the 4-bit computer


addition process actually work?
• We know that if regarded as a binary no, the
computer repn of n is either n or n + 16
• Thus, if 2 repns are added, the result will be
a binary no that is either the correct answer,
or 16 or 32 more than the correct answer
• Ignoring a 1 in the 5th column is equivalent
to subtracting 16
• So, after the process, we have a 4-bit binary
no (i.e. in the range 0-15) that is the correct
answer or differs from it by a multiple of 16
4

Why addition works (continued)


• However, the correct answer is known to be
in the range from –8 to 7
• Thus:
– If the 4-bit binary no is in the range 0-7,
it is the correct answer
– If the 4-bit binary no is in the range 8-15,
it is 16 more than the correct answer
• Either way, the result of the process is the
computer repn of the correct answer – so
the addition process actually works!
5

The Overflow Problem


• What happens if we add nos whose sum lies
outside the permitted range from –8 to 7?
• Example: Find 4 + 7 using a 4-bit computer
• Answer: The computer calculates the sum
to be 1011, which is the repn of –5!
• In this example, 2 pos nos have a sum that
is neg – i.e. the 2 nos being added have sign
bits of 0, but the sign bit of the answer is 1
• This illustrates an overflow problem
6

2
Discrete Mathematics 2002 Lecture 6, 2-August-2002

Dealing with Overflow


• An overflow problem also occurs when 2 neg
nos have a sum that is pos or zero – i.e. the sign
bit of the answer is 0, when the 2 nos being
added have sign bits of 1
• To deal properly with overflow, a 4-bit computer
should generate an error message when it occurs
• In practice, some applications software generates
an error message when overflow occurs, while
some other software does not
• Thus: Be alert for overflow when programming!
7

Subtraction in a Computer
• Subtraction of integers using computer
repns is straightforward because:
– the subtraction of b from a is the same as
adding –b to a (i.e. a – b = a + (–b) ), and
we already know how to add nos
– the repn of –b is the 2’s complement of
the repn of b (except for the repn of –8)
• Thus: Subtraction is performed by adding
the 2’s complement of the number
8

Example of Subtraction
• Example: Illustrate how a 4-bit computer
evaluates 7 – 4
• Why is the the 2’s complement method used
for subtraction in a computer?
Answer: The method does not require a
computer to have ‘subtracters’ in its
circuitry – all it needs are ‘adders’ (and a
means for calculating 2’s complements)
9

3
Discrete Mathematics 2002 Lecture 6, 2-August-2002

3.4 Representing Real Numbers

• We’ve looked at how integers are stored &


manipulated in a computer – we’ll now look
at how real numbers are handled
• Computer representation of real nos is based
on exponential notation

10

Exponential Notation
• If you had 12g of carbon-12, you’d have
6.0221367 × 1023 atoms (Avogadro’s no.)
• This is not written as 60221367 followed by
16 zeros, for 2 reasons – it is inconvenient
& it is wrong (or, at least, quite misleading)
• It is wrong because from the way the no. is
written, all we can say is that it is between
6.02213665 × 1023 and 6.02213675 × 1023
• So writing 60221367 followed by 16 zeros
could be in error by up to 5 million billion!
11

Exponential Notation (continued)


• 6.0221367 × 1023 is said to be written in
exponential notation
• This notation is especially useful if nos are
very big or very small, & there is uncertainty
due to measurement errors or limitations
• In the above no., 6.0221367 is the significand,
10 is the base & 23 is the exponent
• If the significand m is in the range 0.1 ≤ m < 1,
the repn of the no. is said to be normalised
• Thus 6.0221367 × 1023 is not normalised
• However, 0.60221367 × 1024 is normalised
12

4
Discrete Mathematics 2002 Lecture 6, 2-August-2002

Computer Repn of Real Numbers


• Exponential notation is similar to how real
nos are represented in a computer, except
powers of 2 (not 10) are used, and the
exponent is stored in a modified form
• Before finding the computer repn of a real
no., it must firstly be converted to binary,
then expressed in normalised binary
exponential form
13

Normalised Binary Exponential Form


• A no. is in normalised binary exponential form
if it is expressed as ± m × 2e, where:
m is a binary no. in the range 0.12 ≤ m < 1,
e is a decimal integer
• Note that 0 cannot be written in this form
• Example: Express 101110.1012 in nbe form
Answer: 0.1011101012 × 26
• Exercise: Express 0.000011012 in nbe form
Answer: 0.11012 × 2−4
14

Вам также может понравиться