Professional Documents
Culture Documents
COMPUTER ARITHMETIC
Data Representation, Hardware and software
implementation of arithmetic unit for common arithmetic
operations: addition, subtraction, multiplication,
division( Fixed point and floating point); Conversion
between integer and real numbers; Representation of non-
numeric data (character codes, graphical data).
Introduction
Data Representation
Summarize.
Questions?
DATA REPRESENTATION
Binary
Numeric Decimal
Information
Floating Point
Data Hexa Decimal
Non Numeric
DATA REPRESENTATION
Data Types
Complements
Character Codes
DATA TYPES
Computer is dealing with Instructions( operation or control words) and
Data (operands)
Data types are classified as:
n 1
- V(AR ) =
i
a R
i m
i
Radix point(.) separates the integer
portion and the fractional portion
sign magnitude
SIGNED MAGNITUDE REPRESENTATION (CONT’)
.
Examples:
12
NEGATIVE NUMBERS:
SIGN-AND-MAGNITUDE (CONT’)
15
WHY IS IT CALLED “ONE’S COMPLEMENT?”
Complementing a single bit is equivalent to subtracting it from 1.
16
1S COMPLEMENT
ADDITION/SUBTRACTION
Algorithm for addition, A + B:
1. Perform binary addition on the two numbers.
2. If there is a carry out of the MSB, add 1 to the result.
3. Check for overflow: Overflow occurs if result is opposite sign
of A and B.
18
ONE’S COMPLEMENT SUBTRACTION
Example:
(-7)-(-4)
-7 = 1000(A)
-4 = 1011(B)
A-B=A+(-B)
-B = 0100
A+(-B) = 1000
0100
1100 sign bit =1, therefore the result is negative value.[1100 is
the 1’s complement of -3)
TWO’S COMPLEMENT
Our final idea is two’s complement. To negate a number, complement each bit
(just as for ones’ complement) and then add 1.
Examples:
11012 = 1310 (a 4-bit unsigned number)
0 1101 = +1310 (a positive number in 5-bit two’s complement)
1 0010 = -1310 (a negative number in 5-bit ones’ complement)
1 0011 = -1310 (a negative number in 5-bit two’s complement)
20
2S COMPLEMENT
ADDITION/SUBTRACTION
+3 0011 -2 1110
+ +4 + 0100 + -6 + 1010
---- ------- ---- -------
+7 0111 -8 11000
---- ------- ---- -------
+6 0110 +4 0100
+ -3 + 1101 + -7 + 1001
---- ------- ---- -------
+3 10011 -3 1101
---- ------- ---- -------
Which of the above is/are overflow(s)?
OVERFLOW
If the numbers are unsigned, overflow occurs when there is a
carry out of the most significant bit
For signed numbers, overflow detected by comparing the sign
of the result against the sign of the numbers
If one number is positive and another is negative, overflow
cannot occur
If both are positive or both are negative, compare the carry into
the sign bit and carry out of the sign bit
If these two carries are not equal, then there is an overflow
SIGNED OVERFLOW
With two’s complement and a 4-bit adder, for example, the largest represent
able decimal number is +7, and the smallest is -8.
What if you try to compute 4 + 5, or (-4) + (-5)?
0100 (+4) 11 0 0 (-4)
+ 0101 (+5) + 1 0 1 1 (-5)
01001 (-7) 1 0 1 1 1 (+7)
We cannot just include the carry out to produce a five-digit result, as for
unsigned addition. If we did, (-4) + (-5) would result in +23!
Also, unlike the case with unsigned numbers, the carry out cannot be used to
detect overflow.
Inthe example on the left, the carry out is 0 but there is overflow.
Conversely, there are situations where the carry out is 1 but there is no overflow.
24
DETECTING SIGNED OVERFLOW
The easiest way to detect signed overflow is to look at all the sign bits.
25
SIGNED OVERFLOW
Here are all the 4-bit numbers Decimal S.M. 1’s comp. 2’s comp.
in the different systems.
7 0111 0111 0111
Positive numbers are the same 6 0110 0110 0110
in all three representations. 5 0101 0101 0101
Signed magnitude and one’s 4 0100 0100 0100
complement have two ways of 3 0011 0011 0011
representing 0. This makes 2 0010 0010 0010
things more complicated. 1 0001 0001 0001
Two’s complement has 0 0000 0000 0000
asymmetric ranges; there is -0 1000 1111 —
one more negative number
-1 1001 1110 1111
than positive number. Here,
you can represent -8 but not
-2 1010 1101 1110
+8. -3 1011 1100 1101
-4 1100 1011 1100
However, two’s complement
-5 1101 1010 1011
is preferred because it has
only one 0, and its addition
-6 1110 1001 1010
algorithm is the simplest. -7 1111 1000 1001
-8 — — 1000
27
RANGES OF THE SIGNED NUMBER SYSTEMS
How many negative and positive numbers can be represented in each of the different systems on the
previous page?
In general, with n-bit numbers including the sign, the ranges are:
F = EM
- Mantissa
Signed fixed point number, either an integer or a fractional number
- Exponent
Designates the position of the radix point
Decimal Value
Example
A binary number +1001.11 in 16-bit floating point number representation
(6-bit exponent and 10-bit fractional mantissa)
0 0 00100 100111000
Sign Exponent Mantissa
or
0 0 00101 010011100
Normal Form
MSB (3 bits)
0 1 2 3 4 5 6 7