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

Discrete Mathematics 2002 Lecture 4, 30-July-2002

2.5 Arithmetic in Non-Decimal


Bases
• At the end of last lecture we discussed
addition & subtraction of binary nos
• Example: 10112 + 110012 = 1001002
• Example: 1001012 – 11102 = 101112
• Exercise: 1000112 – 110102
Answer: 10012
1

Multiplication in Binary
• The basic table is × 0 1
very easy to write 0 0 0
down 1 0 1
• The usual method of ‘long multiplication’
for decimal nos applies also to binary nos
• Example: 101112 × 11012 = 1001010112
• Exercise: Calculate 10112 × 10102
Answer: 11011102
2

Division in Binary
• Again, the usual method for ‘long division’
applies to division of binary nos
• Example: 111012 ÷ 1102 = 100.110….2
• Note that at each step of the division, the
divisor 1102 ‘goes into’ the number either
once (if it is less than or equal to the number)
or zero times (if it is greater than the number)
3

1
Discrete Mathematics 2002 Lecture 4, 30-July-2002

CHAPTER 3
• Chapter 2 – we looked at the binary system
& its useful shorthands (octal, hex), and at
ways for manipulating these nos by hand
• Chapter 3 – we will look at how binary nos
are stored and manipulated in a computer
• In a computer, integers & real nos are
handled differently – we will concentrate on
integers, & later look only briefly at real nos
4

3.2 Representing Integers


(in a Computer)
• In a computer, an integer is stored in a sequence
of bytes (where 1 byte = 8 bits)
• For manipulation of nos in the processor, 4 bytes
(32 bits) are typically available for each integer
• For convenience, assume 2 bytes (16 bits) are
used – the principles are the same as for 4 bytes
• With 2 bytes, the storage of an integer can be
pictured as

Range of Integers in a Computer


• Each bit can store either 0 or 1 (i.e. 2 nos)
• Thus the total no. of different integers that can be
stored in 16 bits is 216 = 65,536
• Integers in the range 0 ≤ n ≤ 65,535 could be
stored in 2 bytes
• However, it’s preferable to have about the same
no. of pos & neg integers (so subtraction can be
carried out by adding the negative of the number)
• Hence the range –32,768 ≤ n ≤ 32,767 is
preferred (this range is –215 ≤ n ≤ 215–1)
• With 4 bytes, the range is –231 ≤ n ≤ 231 – 1 6

2
Discrete Mathematics 2002 Lecture 4, 30-July-2002

Storing an Integer n in 16 Bits


• 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 15 bits are the binary
repn of n (with leading zeros if necessary)
• If n < 0, the remaining 15 bits are the binary
repn of n + 32,768
• This method for storing neg nos may seem
unnecessarily complicated – but it leads to a
simpler process for arithmetic (as we’ll see)
7

Representing a Negative Integer


• Example: To find the 16-bit computer repn
of –7132, firstly convert to binary:
–7132 = –11011110111002
Now add 32,768 = 10000000000000002 :
–7132 + 32,768 = 1100100001001002
Finally, include the sign bit – so the 16-bit
computer repn is 1110010000100100
• N.B. This is a computer repn, not a binary no.
– so it is not written with subscript ‘2’
8

The 2’s Complement


• There is a quick way of calculating the sum
–7132 + 32,768 = 1100100001001002 :
Write down 7132 as a 15-bit binary number, 7132
= 0011011110111002. Now retain all 0s at the
right end & the rightmost 1, and change all other
bits (0 to 1 & 1 to 0).
• The result is the (15-bit) 2’s complement of 7132
with respect to base 2
• The 2’s complement can be used to find 16-bit
computer repns of nos from –1 to –32767 9

3
Discrete Mathematics 2002 Lecture 4, 30-July-2002

Computer Representations
• Examples: Find the 16-bit computer
representations of the following integers:
(a): 12723 (which is 110001101100112)
(b): –19464 (i.e. −1001100000010002)
(c): –32768 (i.e. –10000000000000002)
• Exercise: Find the 16-bit computer repn of
–5131 (i.e. –10100000010112)
Answer: 1110101111110101
10

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