Upload
meljun-cortes-mbampa
View
215
Download
0
Embed Size (px)
Citation preview
7/29/2019 MELJUN CORTES Binary Calculations
1/21
Lesson 2 - 1
Year 1
CS113/0401/v1
LESSON 2
COMPUTER-BASEDCALCULATIONS
Computers store numbers inbinary
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!
7/29/2019 MELJUN CORTES Binary Calculations
2/21
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
7/29/2019 MELJUN CORTES Binary Calculations
3/21
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
7/29/2019 MELJUN CORTES Binary Calculations
4/21
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
7/29/2019 MELJUN CORTES Binary Calculations
5/21
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
7/29/2019 MELJUN CORTES Binary Calculations
6/21
Lesson 2 - 6
Year 1
CS113/0401/v1
SHIFTING FOR
MULTIPLICATION (2)
Binary shift left n places multiplesby 2n
Fill in right hand side with zeros
Beware the sign bit!
Repeated doubling cannotchange a numbers sign, but it
can send it out of range
Computers have a specialregister to detect this
7/29/2019 MELJUN CORTES Binary Calculations
7/21
Lesson 2 - 7
Year 1
CS113/0401/v1
SHIFTING FOR DIVISION
7/29/2019 MELJUN CORTES Binary Calculations
8/21
Lesson 2 - 8
Year 1
CS113/0401/v1
NUMBER STORAGE IN THE
COMPUTER
Computer store is arranged inwords
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
7/29/2019 MELJUN CORTES Binary Calculations
9/21
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
7/29/2019 MELJUN CORTES Binary Calculations
10/21
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
0 0 1 0 1 1 0 11111
0 0 1 0 1 1 0 11111
7/29/2019 MELJUN CORTES Binary Calculations
11/21
Lesson 2 - 11
Year 1
CS113/0401/v1
Small numbers stored in 12-bitwords 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)
7/29/2019 MELJUN CORTES Binary Calculations
12/21
Lesson 2 - 12
Year 1
CS113/0401/v1
STOREGE OF INTEGERS (3)
The upper limit for storing anumber in a 12-bit word is
011111111111 = 2047 decimal
Larger numbers need double
length
7/29/2019 MELJUN CORTES Binary Calculations
13/21
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
7/29/2019 MELJUN CORTES Binary Calculations
14/21
Lesson 2 - 14
Year 1
CS113/0401/v1
STORAGE OF FRACTIONS
(2)
if 11 bits is not enough for exactrepresentation
Truncate
Round off
Extend to double length (or more)
7/29/2019 MELJUN CORTES Binary Calculations
15/21
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
7/29/2019 MELJUN CORTES Binary Calculations
16/21
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 POINTNOTATION)
7/29/2019 MELJUN CORTES Binary Calculations
17/21
Lesson 2 - 17
Year 1
CS113/0401/v1
TYPES OF NUMBERS
Even if you know that the data isnumeric, make sure you have the
right format
7/29/2019 MELJUN CORTES Binary Calculations
18/21
Lesson 2 - 18
Year 1
CS113/0401/v1
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
7/29/2019 MELJUN CORTES Binary Calculations
19/21
Lesson 2 - 19
Year 1
CS113/0401/v1
STORAGE OF NEGATIVE
VALUES (2)
Conversion from twos
complement to decimal
7/29/2019 MELJUN CORTES Binary Calculations
20/21
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
7/29/2019 MELJUN CORTES Binary Calculations
21/21
Lesson 2 - 21
Year 1
CS113/0401/v1
TWOS COMPLEMENT
SUBTRACTION