Associate Professor School of Computer Science and Engineering (SCOPE) VIT Vellore Module -2 • Data representation and computer arithmetic • Fixed point representation of numbers • Algorithms for arithmetic operations • Multiplication (Booths and modified Booths) • Division (storing and restoring) • Floating point representation with IEEE standards • Algorithms for common arithmetic operations • Representation of non numeric data (character) Unsigned Number • Consider an n-bit binary number B bn i bn 2 .....b1b0 where bi 0 or 1 for 0 i n 1 This vector can represent unsigned integer value V in the range 0 to 2 n 1 where V ( B) bn1 2n1 ..... b1 21 b0 20 Therefore we need to represent both positive and negative numbers Signed Integer • 3 major representations: Sign and magnitude One’s complement Two’s complement • Assumptions: 4-bit machine word 16 different values can be represented Roughly half are positive, half are negative Sign and Magnitude Representation -7 +0 -6 1111 0000 +1 1110 0001 -5 +2 + 1101 0010 -4 1100 0011 +3 0 100 = + 4
-3 1011 0100 +4 1 100 = - 4
1010 0101 -2 +5 - 1001 0110 -1 1000 0111 +6 -0 +7 High order bit is sign: 0 = positive (or zero), 1 = negative Three low order bits is the magnitude: 0 (000) thru 7 (111) Number range for n bits = +/-2n-1 -1 Two representations for 0 One’s Complement Representation -0 +0 -1 1111 0000 +1 1110 0001 -2 +2 + 1101 0010 -3 1100 0011 +3 0 100 = + 4
-4 1011 0100 +4 1 011 = - 4
1010 0101 -5 +5 - 1001 0110 -6 1000 0111 +6 -7 +7
• Subtraction implemented by addition & 1's complement
• Still two representations of 0. This causes some problems • Some complexities in addition One’s Complement Representation Two’s Complement Representation -1 +0 -2 1111 0000 +1 1110 0001 -3 +2 + 1101 0010
• One more negative number than positive number Binary, Signed-Integer Representations Examples: Binary Addition using 1’s Complement A. Addition of a positive and a negative binary number
Case I: When the positive
number has greater magnitude.
In this case addition of numbers
is performed after taking 1’s complement of the negative number and the end-around carry of the sum is added to the least significant bit. Examples: Binary Addition using 1’s Complement A. Addition of a positive and a negative binary number
Case II: When the negative number
has greater magnitude.
In this case the addition is carried in
the same way as in case 1 but there will be non end-around carry. The sum is obtained by taking 1’s complement of the magnitude bits of the result and it will be negative. Examples: Binary Addition using 1’s Complement A. When the two numbers are negative
For the addition of two negative
numbers 1’s complements of both the numbers are to be taken and then added. In this case an end-around carry will always appear. This along with a carry from the MSB will generate a 1 in the sign bit. 1’s complement of the magnitude bits of the result of addition will give the final sum. Examples: Binary Subtraction using 1’s Complement
The steps to be followed in subtraction
by 1’s complement are:
i) Write down the 1’s complement of the
subtrahend. ii) Add this with the minuend. iii) If got a carry, add the carry to its LSB. Else take 1's complement of the result which will be negative Examples: Binary Addition using 2’s Complement A. Addition of a positive number and a negative number.
Case I: When the positive number has a
greater magnitude
Find the 2's complement of the negative
number Add both the numbers By adding both numbers, we get the end- around carry 1 and discard the end- around carry Examples: Binary Addition using 2’s Complement A. Addition of a positive number and a negative number.
Case II: When the negative number
has a greater magnitude
When the negative numbers is greater
no carry will be generated in the sign bit. The result of addition will be negative and the final result is obtained by taking 2’s complement of the magnitude bits of the result.. Examples: Binary Addition using 2’s Complement
B. When the numbers are negative
When two negative numbers are
added, Find the 2's complement of the negative numbers and add the same A carry will be generated from the sign bit which will be discarded. 2’s complement of the magnitude bits of the operation will be the final sum. Examples: Subtraction using 2’s Complement
The operation is carried out by means
of the following steps:
(i) At first, 2’s complement of the
subtrahend is found. (ii) Then it is added to the minuend. (iii) If the final carry over of the sum is 1, it is dropped and the result is positive. (iv) If there is no carry over, the two’s complement of the sum will be the result and it is negative Examples: Subtraction using 2’s Complement
The operation is carried out by means
of the following steps:
(i) At first, 2’s complement of the
subtrahend is found. (ii) Then it is added to the minuend. (iii) If the final carry over of the sum is 1, it is dropped and the result is positive. (iv) If there is no carry over, the two’s complement of the sum will be the result and it is negative More Examples: Addition and Subtraction – 2’s Complement 2’s-Complement Add and Subtract Operations Problems for Practice Find the sum of the following by using 2’s complement: 1. + 0111 and – 0011 = 0100 2. + 0100 and -0111 = -0011 3. -0111 and – 0010 = -1001
Do subtraction using 2’s complement:
1. +0111 and -0011 = 0100 2. -0101 and -1010 = -0001 3. 1010.11 – 1001.01 = 1.10 Multiplication of unsigned numbers
Product of 2 n-bit numbers is at most a 2n-bit number.
Unsigned multiplication can be viewed as addition of shifted versions of the multiplicand. Multiplication of Unsigned Binary Integers