You are on page 1of 16

CHAPTER 2 : COMPUTER BASED ARITHMETIC

Chapter Objectives
At the completion of this chapter, you should understand how to: perform binary calculation; store numbers into computer words; integer fraction mixed number;

store numbers into computer words using; sign modulus method 2s complement method;

perform 2s complement subtraction; shift operations.

2-1

CHAPTER 2 : COMPUTER BASE ARITHMETIC

CS113

2.1

Binary Addition
Binary addition for fixed point numbers is done as follows: The numbers to be added are aligned by their binary points. Starting from the least significant (rightmost) digit, a corresponding pair of digits (in the same column) are added according to the rules of binary addition. If a column has a carry from the previous column, the carry digit has to be added also. Repeat steps (ii) and (iii) for each column until the most significant (leftmost) digit has been added.

2.1.1 Rules for Binary Addition


1+1 1+0 0+1 0+0 = = = = 0 1 1 0 and carry over 1 to add to next column (i.e. 1+1 = 10)

2.1.2 Examples of Binary Addition


Example: Binary 1010 0111 10001 Decimal equivalent 10 + 7 17

+ Example:

+ Example:

Binary 1010.01 0110.11 10001.00

Decimal equivalent 10.25 + 6.75 17.00

Binary
111 carry over

Decimal equivalent
1 carry over

101100 011101 1001001

44 29 73

2-2

CS113

CHAPTER 2 : COMPUTER BASE ARITHMETIC

Example: Binary
11 carry over

Decimal equivalent 0.11 0.01 1.00 0.75 0.25 1.00

2.2

Binary Subtraction
2.2.1 Rules for Binary Subtraction
0-0 1-1 1-0 0-1 = = = = 0 0 1 1

with borrow 1 from the column to the left of the number subtracted from

2.2.2

Examples of Binary Subtraction


Binary 11010 01001 10001 Decimal equivalent 26 9 17

Example:

Example:

Binary 1010.00 1000.11 00001.01

Decimal equivalent 10.00 8.75 1.25

The subtraction process can be more clearly illustrated in the following example. Example: Step 1
1

Step 2
11

Step 3
011

Step 4
011

11000 00001

11000 00001 1

11000 00001 1

11000 00001 1

11000 00001 10111

Borrow changes 0 to 1

Continue changing 0s to 1s

Until a 1 can be changed to a 0, then proceed with subtraction

2-3

CHAPTER 2 : COMPUTER BASE ARITHMETIC

CS113

2.2.3 Examples on Successive Borrows in Binary Subtractions


Example: 810 - 210 = 610
0 1

1 0 0

0 0 1

0 1 1

0 0 0

= = =

810 210 610

Example:

910 - 710 = 210


0 1

1 0 0

0 1 0

0 1 1

1 1 0

= = =

910 710 210

2.3

Binary Multiplication
A x B C = = = Multiplicand Multiplier Product

2.3.1 Rules for Binary Multiplication


0x0 0x1 1x0 1x1 = = = = 0 0 0 1

Copy the multiplicand when the multiplier digit is 1; do not when it is 0. Shift as in decimal multiplication. Add the resulting binary numbers according to the binary addition rules.

2-4

CS113

CHAPTER 2 : COMPUTER BASE ARITHMETIC

2.3.2

Examples of Binary Multiplication

Example: Binary 1101 1100 0000 0000 1101 1101 10011100 x Example: Binary 101 111 101 101 101 100011 x Decimal equivalent x 5 7 35 Decimal equivalent x 13 12 26 13 156

2.3.3

Handling of Carries in Multiplication


x 0 1 1 0 1 1 1 0 0 1 0 1 1 1 1 0 1 1 1 1 1 0 1 1 1 1 1 1 1 1

0 1

0 1 1

1 carries

The product is 11010012

2-5

CHAPTER 2 : COMPUTER BASE ARITHMETIC

CS113

Example:

410 x 310 = 1210 0 x 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 1 0 02 = 1210 1 0 1 0 0 0 1 0 0 02 12 0 = = 410 310

Example:

1010 x 1410 = 14010 1 x 1 1 1 1 0 0 0 0 1 0 1 0 0 1 0 1 1 0 02 = 14010 0 1 0 1 0 1 1 0 0 02 02 0 = = 1010 1410

2.4

Binary Division
Just as binary multiplication is a series of shift and add operations, binary division is a series of shift and subtract operations. divisor 101.1 110 ) 10001.0 110 1001 110 110 110 quotient dividend

Binary Add digit from dividend to group used above. Subtraction possible, so put 1 in quotient. Remainder from subtraction possible, so put 1 in quotient. Perform subtraction, and add digit from dividend to remainder. Subtraction possible, so put 1 in quotient. No remainder, so stop.

2-6

CS113

CHAPTER 2 : COMPUTER BASE ARITHMETIC

Decimal Equivalent 5.5 6 ) 33 30 30 30

2.4.1

Example of Binary Division


3210 = (102/3)10 or 10 remainder 2 310
1010.10 11) 100000.00 11 100 11 100 11 10 remainder

Example:

2.5

Number Storage in the Computer Word


Computer storage is arranged in words. A word is defined as a unit of data for the machine. Words vary in length on different types of machines. Common word sizes are 8, 12, 16, 24 and 32 bits.

Bit

A bit, short for binary digit, is in fact the smallest unit of information stored in a circuit element such as a gate or moved through the circuitry as a pulse or no-pulse. By itself, it is of course limited in its capacity to carry information. A 8-bit string of binary bits is known as a byte. It is widely used because it corresponds to one character. A word is usually a multiple number of bytes. It is machine dependent and it is the unit of data for the machine. e.g. 8-bits, 16-bits, 32-bits, etc. Characters may be represented by binary codes e.g. ASCII code. Since character codes are normally no more than 8 bits in length, there is a tendency to substitute byte for character and character for byte rather indiscriminately when describing storage capacities.

Byte Word

Character

2-7

CHAPTER 2 : COMPUTER BASE ARITHMETIC

CS113

2.5.1 16 Bit Microcomputers


Most of the popular microcomputer based systems used in schools and colleges are 16-bit micros. In these machines each word in memory is 2 bytes in length (i.e. 8-bits). Each word is sufficiently large to hold the binary code for one character but program instructions are normally too long to fit into a single word and are therefore spread over two or three words in sequence.

2.5.2 32 Bit Machines


Some of the more modern microcomputers are 32-bit micros. Many minicomputers are also 32-bit machines. Each word is four bytes (32-bits) in length, i.e. the word length is 32-bits.

2.5.3 Size Limits of Data


Computer storage is of finite size. This limits the size of numbers which can be stored and their accuracy. Example: 16 bit words are common and hold numbers from -32768 to 32767.

2.6

Storage of Numbers
It is important to know the format of storage before interpreting the given 0s and 1s stored in a computer word. There are many formats of storing numbers: Integer (using sign modulus format) Integer (using 2s complement format) Fraction Mixed number Floating point number Fixed point format

2.6.1 Storage of Integers Using Sign Modulas Method


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

[00110101|11000000] = = = 13760 0.2099609375 53.75 decimal integer decimal fraction decimal mixed

and there are many other formats

2-8

CS113

CHAPTER 2 : COMPUTER BASE ARITHMETIC

2.6.2

Storage of Integers

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 Sign bit For sign modulus method 0 1 Positive Negative 0 1 1 0 1 1 1 1 1

Small numbers stored in 12-bit words are mostly padding bits 0 0 0 0 0 0 0 0 1 0 0 1

Sign bit

Padding

Decimal

Padding is not really wasteful, it is necessary to make the calculation methods work properly The upper limit for storing a number in a 12-bit word is 011111111111 = 2047 decimal The range of a given computer n bit word for sign modulus is -(2n-1) to (2n-1 - 1) Larger numbers need double length 0 0 0 0 0 0 0 0 0 0 1 1 Sign bit 7649 decimal = 1 0 1 1 1 1 0 0 0 0 1 This bit is ignored 1110111100001

22 bits can hold 419430310

2-9

CHAPTER 2 : COMPUTER BASE ARITHMETIC

CS113

2.7

Storage of Fractions
First bit still sign bit Bicimal point not stored Implied after sign bit Decimal 0.4140625 0 0 1 1 0 1 0 1 0 0 0 0

Sign bit

Implied bicimal point

padding

This representation is exact. If 11 bits is not enough for exact representation: Truncate Round off Extend to double length (or more)

Double length fraction: 0.7328 decimal 0 1 0 1 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 0 0

Sign bit

Implied bicimal

Redundant bit Truncated to fit

Double length fractions have increased accuracy, not range

2.8

Storage of Mixed Numbers


(Fixed Point Notation) Single word: 0 0 1 0 1 1 1 11.75 decimal 1 0 0 0 0

Sign bit Divide as necessary

Implied bicimal point

2 - 10

CS113

CHAPTER 2 : COMPUTER BASE ARITHMETIC

Double word: 74.890625 decimal 0 0 0 0 0 1 0 0 1 0 1 0 1 1 1 0 0 1 0 0 0 0 0

Sign bit

Implied bicimal point

Redundant

Usual convention is one word for integral part, the other for fraction

2.9

Storage of Number Using 2s Complement Method


Sign-modulus method is unsuitable for calculation. Computers usually use 2s complement method to store numbers and calculation. In 2s complement method, no sign bit is reserved to indicate the sign of number. However, if the leftmost bit is 0, it indicates a positive number. If the leftmost is a 1, then it is an implied negative number. Stored numbers in 2s complement method can be defined in 4 stages. Steps: 1. 2. 3. 4. Convert absolute value of given no. into binary. Store binary no. into given computer word. Check if given no is positive, the steps are completed. If given no is negative then 4.1 Invert all bits 0 1 1 0

4.2 Example: To store 45: Steps: 1. 2. 3.

Add 1 to the inverted no. Store 45 and -45 into 8 bit register

45 in binary : 101101 45 in 8 bit : 00101101 Since no. (45) is positive, task is completed.

2 - 11

CHAPTER 2 : COMPUTER BASE ARITHMETIC

CS113

To store -45: Steps: 1. 2. 3. 45 in binary : 45 in 8 bits: -45: 101101 00101101 11010010 + 1 11010011 The range of number that can be stored in n-bit comp word in 2s complement format is - (2n-1) to (2n-1 - 1) Example: in 8 bit word, the range is - (28-1) to (28-1 - 1) = -128 to 127

2.10 Tens Complement


8 = 2 because 8 + 2 = 10 as such 8 - 2 = (8 + 8) - 10 = 6 6 - 4 = (6 + 6) - 10 = 2 These findings have a significant impact on the arithmatics operation in computer.

2.11 Twos Complement


Computers can only do addition, so subtraction is done by adding the complement of the subtrahend to the minuend. (Using four bits to store the binary number) Example: 5 convert to binary 0101 3 convert to binary 0011 1100 step 1 invert the bits of 0011 + 1 step 2 add 1 to the least significant bit of 1100 1101 step 3 add the 2s complement of 0011 to 0101 2 carry 1 ignored + 0101 0010 convert to denary is 2

2 - 12

CS113

CHAPTER 2 : COMPUTER BASE ARITHMETIC

2.12 Recomplementing Cases


If the minuend is lesser than the subtrahend then it is necessary to recomplement the answer and add a minus sign to it. Example: 3 convert to binary 0011 -5 convert to binary 0101 1010 step 1 invert the bit of 0101 + 1 step 2 add 1 to the least significant bit of 1010 -2 1011 step 3 add the 2s complement of 0101 to 0011 0011 1110 step 4 recomplement 1110 and add a minus 0001 sign to the answer + 1 -0010 convert to denary is -2

2.13 Shift Operations


Shifting refers to a process whereby it is possible to physically move the pattern of bits within a computer word to the left or to the right. A number of different types of shift operations do exist. The most common are circular, logical and arithmetic shifts. We will only be interested in the arithmetic shift operation.

2.13.1 Shift Operations to Achieve Multiplication


If every bit in a 12-bit word is moved one place to the left it immediately has an associated place-value which is twice that previously associated with it and so the effect of shifting one place to the left is to double the value of the quantity originally stored. If shifted two places to the left, this doubling is repeated, so that the result is now four times the original; three place shifted to the left has the effect of multiplying the quantity by eight (since 8 = 23) and so on. Under these circumstances the gaps which occur at the right hand end of the word as the shifting takes place are filled with zeros and anything shifted beyond the left-hand end is lost. Additionally, this process is only valid so long as the sign-bit remains as zero, (although techniques do exist to cope with negative quantities). But if a positive number is to be multiplied in this way and there is possible to avoid the difficulty by going into double-length.

2 - 13

CHAPTER 2 : COMPUTER BASE ARITHMETIC

CS113

Example: original binary number 0 0 0 0 0 0 + 50 1 place shift to the left 0 0 0 0 0 1 1 0 0 1 0 0 1 1 0 0 1 0

+ 100 = (50 x 21) 2 places shift to the left 0 0 0 0 1 1 0 0 1 0 0 0

+ 200 = (50 x 22) 3 places shift to the left 0 0 0 1 1 0 0 1 0 0 0 0

+ 400 = (50 x 23)

2.13.2 Shift Operations to Achieve Division


Just as shifting to the left effects multiplication by the relevant power of two, so shifting to the right achieves division by the relevant power of two. In this case the gaps caused by shifting occur at the left-hand end of the word and are replaced by copies of the sign-bit to retain arithmetic significance: it is in fact more straightforward to cope with negative values here than in the case of multiplication. Any bit shifted beyond the right-hand end of the word is lost and the result is a truncated form of division. If the last bit to be shifted beyond the right-hand end of the word were to be then added back on to the right-hand end bit the result would be a rounded form of the same division.

2 - 14

CS113

CHAPTER 2 : COMPUTER BASE ARITHMETIC

2.14 Past Years Questions


1. If a computer stores integers in one byte using the twos complement: a. b. c. 2. a. b. What range of integers can be represented? Show how the computer would evaluate -100 -40. You must show all working. Briefly comment on your results in (b). [1] [5] [1]

Determine the range of integer that could be stored in a 6 bits register in twos complement form. Using twos complement, evaluate the following numbers which are stored in 5 bits registers, evaluate the following: i. ii. 12 - 15 15 - 10 [2] [2]

3.

A particular computers 8 bit registers store number in twos complement form. a. b. Show how -96 is represented. Show how 85 -120 would be evaluated. [1] [4]

4.

a. b.

Express 101.101 and -100.11 in binary using 8 bits for the integer part and 8 bits for the (unrounded) fraction part. [4] Convert the fraction 10011100.10100110 to decimal. [4]

5.

Calculate the following using complement subtraction, assuming the numbers are stored in 8-bit registers 13 - 56. [4] a. If numbers are held in an 8 BIT register in TWOs COMPLEMENT form. i. ii. b. Write down the COMPLETE range of INTEGERS which can be held. [2] Show how 96 - 124 would be evaluated. [4]

6.

If a very simple 4 BIT register were to be used. i. ii. What would the binary representation of 0.710 be? What denary value would the register actually hold? [2] [1]

2 - 15

CHAPTER 2 : COMPUTER BASE ARITHMETIC

CS113

7.

Show, in BINARY, how a 12-BIT register would hold. i. ii. The integer -894 using TWOs complement. [4] Show how -894 would be added to ITSELF using TWOs complement and justify that the CORRECT answer is obtained. [3] [1] [1] What is this situation called?

iii. Show what happens if -894 is added yet again. iv. 8.

Using TWOs COMPLEMENT, base 10 integers are to be stored in an EIGHT bit register. a. b. c. d. What range of INTEGERS, (expressed in DENARY form) can be stored? [2] Show how 7110 is represented. Show how -8310 is represented. Show how 7110 -8310 would be evaluated. [1] [2] [3]

9.

a.

If a computer stores integers in two bytes using TWOs complement: i. ii. What is the range of integers which can be represented? [2] How would the computer evaluate 47 -125? Show all your working. [4]

b.

Given 1001 0011 0101 in a 12-bit word, interpret this in denary if it is of format: i. ii. TWOs complement. Fixed point number with 6 decimal places. [3] [3]

2 - 16