Date post: | 10-Jun-2015 |
Category: |
Documents |
Upload: | meljun-cortes |
View: | 86 times |
Download: | 6 times |
Lesson 2 - 1
Year 1
CS113/0401/v1
LESSON 2COMPUTER-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!
Lesson 2 - 2
Year 1
CS113/0401/v1
BINARY ADDITION
0 + 0 = 0 carry 0
0 + 1 = 1 carry 0
1 + 1 = 0 carry 1
1 + 1 + 1 = 1 carry 1
1010102
+ 1110012
11000112 Result = 11000112
111 carry
1 + 1 + 1 + 1 = 0 carry 10 (binary)
1101102
+ 1011012
+ 11110102
11011101
1101111 carry
Lesson 2 - 3
Year 1
CS113/0401/v1
BINARY SUBTRACTION
1 1 10 1 10 1 0 12
- 101 1 10 1 1 0 02
0 1 1 0 1 0 0 12
Result 11010012
This is not the way computers do it
Lesson 2 - 4
Year 1
CS113/0401/v1
SHIFT OPERATIONS
In reality, multiplication and division are done using Shifting
Circular, Logical and Arithmetic shifts exit
We will consider the Arithmetic Shift
Lesson 2 - 5
Year 1
CS113/0401/v1
SHIFTING FOR MULTIPLICATION (1)
Shift LEFT
73510 shifted left 1 place
7 3 5
7 3 5 0
= 735010 = 73510 X 1010
11012 shifted left 1 place
1 1 0 1
1 1 0 1 0
= 110102 = 11012 X 102
Lesson 2 - 6
Year 1
CS113/0401/v1
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 number’s sign, but it can send it out of range
Computers have a special register to detect this
Lesson 2 - 7
Year 1
CS113/0401/v1
SHIFTING FOR DIVISION
Lesson 2 - 8
Year 1
CS113/0401/v1
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
Lesson 2 - 9
Year 1
CS113/0401/v1
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
Lesson 2 - 10
Year 1
CS113/0401/v1
STOREGE OF INTEGERS (1)
735 DECIMAL = 1011011111 (10 bits)
Add 2 bits “padding” in a 12-bit word
By convention, the first (left-hand) bit is the SIGN BIT
Therefore only 11 bits are left for the number value
For sign Modulus method : 0 Positive
1 Negative
0 0 1 0 1 1 0 11111
0 0 1 0 1 1 0 11111
Lesson 2 - 11
Year 1
CS113/0401/v1
Small numbers stored in 12-bit words contain mostly “padding” bits
Padding is not really wasteful, it is necessary to make the calculation method work properly
Sign bit Padding Decimal 9
0 0 0 0 0 0 0 10010
STOREGE OF INTEGERS (2)
Lesson 2 - 12
Year 1
CS113/0401/v1
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
Lesson 2 - 13
Year 1
CS113/0401/v1
STORAGE OF FRACTIONS (1)
First bit still sign bit
Decimal point not stored
Implied after sign bit
Lesson 2 - 14
Year 1
CS113/0401/v1
STORAGE OF FRACTIONS (2)
if 11 bits is not enough for exact representation
Truncate
Round off
Extend to double length (or more)
Lesson 2 - 15
Year 1
CS113/0401/v1
STORAGE OF FRACTIONS (3)
Double length fraction
0.7323 decimal
Double length fractions have increased accuracy, not range
Lesson 2 - 16
Year 1
CS113/0401/v1
Usual convention is one word for integral part, the other for fraction
STORAGE OF MIXED NUMBERS (FIXED POINT
NOTATION)
Lesson 2 - 17
Year 1
CS113/0401/v1
TYPES OF NUMBERS
Even if you know that the data is numeric, make sure you have the right format
Lesson 2 - 18
Year 1
CS113/0401/v1
STORAGE OF NEGATIVE VALUES (1)
Sign-and-modulus method is unsuitable for calculation
Computers usually use two’s complement method
Three stages to finding a two’s complement
Example : -837 decimal
Lesson 2 - 19
Year 1
CS113/0401/v1
STORAGE OF NEGATIVE VALUES (2)
Conversion from two’s complement to decimal
Lesson 2 - 20
Year 1
CS113/0401/v1
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
Lesson 2 - 21
Year 1
CS113/0401/v1
TWO’S COMPLEMENT SUBTRACTION