You are on page 1of 21

Year 1

LESSON 2 COMPUTER-BASED CALCULATIONS

Computers store numbers in binary They calculate in binary Principles of binary calculation are essentially similar to those for decimal When using computers, make sure you know what the binary data means!

CS113/0401/v1

Lesson 2 - 1

Year 1

BINARY ADDITION
0 0 1 1 + + + + 0 1 1 1 + 1 = = = = 0 carry 1 carry 0 carry 1 carry 0 0 1 1

1010102 + 1110012 11000112 111 carry Result = 11000112

1 + 1 + 1 + 1 = 0 carry 10 (binary) 1101102 + 1011012 11011101 + 11110102


CS113/0401/v1 Lesson 2 - 2

1101111

carry

Year 1

BINARY SUBTRACTION

1 -

1
101

10

1
10

10

0 0

12 02

1 1

1 1

12

Result 11010012

This is not the way computers do it

CS113/0401/v1

Lesson 2 - 3

Year 1

SHIFT OPERATIONS

In reality, multiplication and division are done using Shifting Circular, Logical and Arithmetic shifts exit We will consider the Arithmetic Shift

CS113/0401/v1

Lesson 2 - 4

Year 1

SHIFTING FOR MULTIPLICATION (1)


Shift LEFT 73510 shifted left 1 place 7 7 = 3 3 5 5 0

735010 = 73510 X 1010

11012 shifted left 1 place

1 1 = 1

1 0

0 1

1 0

110102 = 11012 X 102


Lesson 2 - 5

CS113/0401/v1

Year 1

SHIFTING FOR MULTIPLICATION (2)

Binary shift left n places multiples by 2n Fill in right hand side with zeros Beware the sign bit! Repeated doubling cannot change a numbers sign, but it can send it out of range Computers have a special register to detect this
CS113/0401/v1 Lesson 2 - 6

Year 1

SHIFTING FOR DIVISION

CS113/0401/v1

Lesson 2 - 7

Year 1

NUMBER STORAGE IN THE COMPUTER

Computer store is arranged in words Words are fixed length groups of binary digits (bits) Words vary in length on different types of computer Common word lengths are 8, 12, 16, 24 and 32 bits We shall use 12 bit words in examples NB : An 8-bit word is called a byte
CS113/0401/v1 Lesson 2 - 8

Year 1

SIZE LIMITS ON DATA

Computer stores are of finite size This limits the range of values which can be stored and the accuracy of fractions Example

16-bit words are common and can hold integers in the range from -32768 to +32767

CS113/0401/v1

Lesson 2 - 9

Year 1

STOREGE OF INTEGERS (1)

735 DECIMAL = 1011011111 (10 bits)

Add 2 bits padding in a 12-bit word


0 0 1 0 1 1 0 1 1 1 1 1

By convention, the first (left-hand) bit is the SIGN BIT Therefore only 11 bits are left for the number value
0 0 1 0 1 1 0 1 1 1 1 1

For sign Modulus method : 0 Positive

CS113/0401/v1

Lesson 2 - 10

Year 1

STOREGE OF INTEGERS (2)

Small numbers stored in 12-bit words contain mostly padding bits

Sign bit

Padding

Decimal 9

Padding is not really wasteful, it is necessary to make the calculation method work properly

CS113/0401/v1

Lesson 2 - 11

Year 1

STOREGE OF INTEGERS (3)

The upper limit for storing a number in a 12-bit word is 011111111111 = 2047 decimal

Larger numbers need double length

CS113/0401/v1

Lesson 2 - 12

Year 1

STORAGE OF FRACTIONS (1)


First bit still sign bit Decimal point not stored

Implied after sign bit

CS113/0401/v1

Lesson 2 - 13

Year 1

STORAGE OF FRACTIONS (2)

if 11 bits is not enough for exact representation

Truncate Round off Extend to double length (or more)

CS113/0401/v1

Lesson 2 - 14

Year 1

STORAGE OF FRACTIONS (3)

Double length fraction 0.7323 decimal

Double length fractions have increased accuracy, not range


CS113/0401/v1 Lesson 2 - 15

Year 1

STORAGE OF MIXED NUMBERS (FIXED POINT NOTATION)

Usual convention is one word for integral part, the other for fraction
CS113/0401/v1 Lesson 2 - 16

Year 1

TYPES OF NUMBERS

Even if you know that the data is numeric, make sure you have the right format

CS113/0401/v1

Lesson 2 - 17

Year 1

STORAGE OF NEGATIVE VALUES (1)

Sign-and-modulus method is unsuitable for calculation Computers usually use twos complement method Three stages to finding a twos complement

Example : -837 decimal

CS113/0401/v1

Lesson 2 - 18

Year 1

STORAGE OF NEGATIVE VALUES (2)

Conversion from twos complement to decimal

CS113/0401/v1

Lesson 2 - 19

Year 1

STORAGE OF NEGATIVE VALUES (3)

Ranges of numbers Largest negative number in 12 bits = 100000000000 = -2048

Total range of 12 bits is -2048 to + 2047 Multiple length works with negative values as well

CS113/0401/v1

Lesson 2 - 20

Year 1

TWOS COMPLEMENT SUBTRACTION

CS113/0401/v1

Lesson 2 - 21