0% found this document useful (0 votes)
113 views41 pages

Unit - II: Computer Arithmetic

This document provides information about different methods for representing numeric data on computers, including fixed point and floating point representations. It discusses fixed point representation using binary numbers and describes several methods for representing signed integers - sign-and-magnitude, one's complement, and two's complement. Two's complement representation is outlined as the preferred approach, as it only has one representation for zero and has the simplest addition algorithm. Ranges of numbers that can be represented are given for different bit sizes using the various signed number systems.

Uploaded by

Riajimin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
113 views41 pages

Unit - II: Computer Arithmetic

This document provides information about different methods for representing numeric data on computers, including fixed point and floating point representations. It discusses fixed point representation using binary numbers and describes several methods for representing signed integers - sign-and-magnitude, one's complement, and two's complement. Two's complement representation is outlined as the preferred approach, as it only has one representation for zero and has the simplest addition algorithm. Ranges of numbers that can be represented are given for different bit sizes using the various signed number systems.

Uploaded by

Riajimin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 41

Unit -II

Computer Arithmetic

01-08-2013

Introduction
Data Representation
Fixed Point Representation
Floating Point Representation
IEEE format Floating Point
Summarize.
Questions?

Data Representation

Data Representation
Data Types
Complements
Fixed Point Representations
Floating Point Representations
Character Codes

Data types
Computer is dealing with Instructions( operation or
control words) and Data (operands)
Data types are classified as:
1. Numbers used in arithmetic computations[Numeric]
2. Letters of alphabets used in data processing [Non
Numeric]
3. Other discrete symbols used for specific
purposes[Non numeric]

Basic Format
Data
- Numeric Data deals with Numbers( Integer, real)
a) Fixed Point
- Binary
- Decimal
b) Floating Point
- Binary
- Decimal
- Non-numeric Data deals with Letters, Symbols

Numeric Data Representation


Number System

Nonpositional number system


- Roman number system

Positional number system


- Each digit position has a value called a weight associated with it
- Decimal, Octal, Hexadecimal, Binary
Base (or radix) R number
- Uses R distinct symbols for each digit
- Example AR = an-1 an-2 ... a1 a0 .a-1a-m
- V(AR ) =

n 1

a R

i m

R = 10 Decimal number system,


R = 8 Octal,

Radix point(.) separates the integer


portion and the fractional portion
R = 2 Binary
R = 16 Hexadecimal

Fixed Point Representation


Numbers:

Fixed Point Numbers and Floating Point Numbers

Binary Fixed-Point Representation


X = xnxn-1xn-2 ... x1x0. x-1x-2 ... x-m
Sign Bit(xn): 0 for positive, 1 for negative
Remaining Bits(xn-1xn-2 ... x1x0. x-1x-2 ... x-m)

Integer-Fixed Point Data


Unsigned Number: only non-negative
values.
Signed numbers: include all values
(positive and negative).

Signed Magnitude
Ones Complement
Twos Complement

Sign-and-Magnitude
Negative numbers are usually written by
writing a minus sign in front.
Example:

- (12)10 , - (1100)2

In sign-and-magnitude representation, this


sign is usually represented by a bit:
0 for +
1 for -

Negative Numbers:
Sign-and-Magnitude (Cont)
Example: an 8-bit number can have 1-bit sign and 7bit magnitude.

sign

magnitude

Signed magnitude representation


(cont)
.

Examples:
11012 = 1310

(a 4-bit unsigned number)

0 1101 = +1310 (a positive number in 5-bit signed magnitude)


1 1101 = -1310

01002 = 410

(a negative number in 5-bit signed magnitude)

(a 4-bit unsigned number)

0 0100 = +410

(a positive number in 5-bit signed magnitude)

1 0100 = -410

(a negative number in 5-bit signed magnitude)

12

Negative Numbers:
Sign-and-Magnitude (Cont)
To negate a number, just invert the sign bit.
Examples:
- (0 0100001)sm = (1 0100001)sm
- (1 0000101)sm = (0 0000101)sm
Overflow in signed magnitude numbers occur when the result occupy more number of bits
Example:

[ 3 bit register for magnitude]

-7= 1 111
-4 =1 100
1011 [result occupies 4 bit hence overflow]

1s and 2s Complement
Two other ways of representing signed
numbers for binary numbers are:
1s-complement
2s-complement

They are preferred over the simple sign-andmagnitude representation.

Ones complement
representation

A different approach, ones complement, negates numbers by


complementing each bit of the number.
We keep the sign bits: 0 for positive numbers, and 1 for negative. The sign
bit is complemented along with the rest of the bits.
Examples:
11012 = 1310

(a 4-bit unsigned number)

0 1101 = +1310 (a positive number in 5-bit ones complement)


1 0010 = -1310
01002 = 410

(a negative number in 5-bit ones complement)


(a 4-bit unsigned number)

0 0100 = +410

(a positive number in 5-bit ones complement)

1 1011 = -410

(a negative number in 5-bit ones complement)

15

Why is it called ones


complement?

Complementing a single bit is equivalent to subtracting it from 1.


0 = 1, and 1 - 0 = 1

1 = 0, and 1 - 1 = 0

Similarly, complementing each bit of an n-bit number is equivalent to


subtracting that number from 2n-1.
For example, we can negate the 5-bit number 01101.
Here n=5, and 2n-1 = 3110 = 111112.
Subtracting 01101 from 11111 yields 10010:
1 1 1 1 1
- 0 1 1 0 1
1 0 0 1 0

16

1s Complement Addition/Subtraction

Algorithm for addition, A + B:

1.

Perform binary addition on the two numbers.


If there is a carry out of the MSB, add 1 to the result.
Check for overflow: Overflow occurs if result is opposite sign
of A and B.

2.
3.

Algorithm for subtraction, A B:


A B = A + (B)

1.

Take 1s complement of B by inverting all the bits.


Add the 1s complement of B to A.

2.

Ones complement addition

To add ones complement numbers:


First do unsigned addition on the numbers, including the sign bits.
Then take the carry out and add it to the sum.
0011
(+3)
Two examples:
+ 0010 + (+2)
1000
(-7)
0 0101
+ 1011 + (-4)
1 0011
0101
+
0
0011
0101
(+5)
+
1
0100
(+4)

since the sign is opposite to the operands


there is a overflow here.
This is simpler and more uniform than signed magnitude addition.
18

Ones 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 1s complement of -3)

Twos complement

Our final idea is twos 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 twos complement)
1 0010 = -1310

(a negative number in 5-bit ones complement)

1 0011 = -1310

(a negative number in 5-bit twos complement)

01002 = 410

(a 4-bit unsigned number)

0 0100 = +410

(a positive number in 5-bit twos complement)

1 1011 = -410

(a negative number in 5-bit ones complement)

1 1100 = -410

(a negative number in 5-bit twos complement)

20

2s Complement Addition/Subtraction

Algorithm for addition, A + B:

1.

Perform binary addition on the two numbers.


Ignore the carry out of the MSB (most significant bit).
Check for overflow: Overflow occurs if the carry in and carry
out of the MSB are different, or if result is opposite sign of A
and B.

2.
3.

Algorithm for subtraction, A B:


A B = A + (B)

1.

Take 2s complement of B by inverting all the bits and adding 1.


Add the 2s complement of B to A.

2.

2s Complement Addition/Subtraction

Examples: 4-bit binary system


+3
+ +4
---+7
----

0011
+ 0100
------0111
-------

-2
+ -6
----8
----

1110
+ 1010
------11000
-------

+6
+ -3
---+3
----

0110
+ 1101
------10011
-------

+4
+ -7
----3
----

0100
+ 1001
------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 twos 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
+ 0101
01001

(+4)
(+5)
(-7)

11 0 0 (-4)
+ 1 0 1 1 (-5)
1 0111
(+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.

In the 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.
0100
+ 0101
01001

(+4)
(+5)
(-7)

1100
+ 1011
10111

(-4)
(-5)
(+7)

Overflow occurs only in the two situations above:


If you add two positive numbers and get a negative result.
If you add two negative numbers and get a positive result.
Overflow cannot occur if you add a positive number to a negative
number. Do you see why?

25

Signed Overflow
carry generated, but no overflow

no carry and no overflow

carry and overflow

no carry and overflow

Comparing the signed number


systems
Here are all the 4-bit
numbers in the different
systems.
Positive numbers are the
same in all three
representations.
Signed magnitude and ones
complement have two ways
of representing 0. This
makes things more
complicated.
Twos complement has
asymmetric ranges; there is
one more negative number
than positive number. Here,
you can represent -8 but not
+8.
However, twos complement
is preferred because it has
only one 0, and its addition
algorithm is the simplest.

Decimal

S.M.

1s comp.

2s comp.

7
6
5
4
3
2
1
0
-0
-1
-2
-3
-4
-5
-6
-7
-8

0111
0110
0101
0100
0011
0010
0001
0000
1000
1001
1010
1011
1100
1101
1110
1111

0111
0110
0101
0100
0011
0010
0001
0000
1111
1110
1101
1100
1011
1010
1001
1000

0111
0110
0101
0100
0011
0010
0001
0000

1111
1110
1101
1100
1011
1010
1001
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?

Smallest
Largest

Unsigned

Signed
Magnitude

Ones
complement

Twos
complement

0000 (0)
1111 (15)

1111 (-7)
0111 (+7)

1000 (-7)
0111 (+7)

1000 (-8)
0111 (+7)

In general, with n-bit numbers including the sign, the ranges are:

Smallest
Largest

Unsigned

Signed
Magnitude

Ones
complement

Twos
complement

0
2n-1

-(2n-1-1)
+(2n-1-1)

-(2n-1-1)
+(2n-1-1)

-2n-1
+(2n-1-1)
28

Pros and cons of integer


representation
Signed magnitude representation:

2 representations for 0
Simple
Need to consider both sign and magnitude in arithmetic
Different logic for addition and subtraction

1s complement representation:
2 representations for 0
Complexity in performing addition and subtraction

2s complement representation:
Only one representation for 0
Arithmetic works easily
Negating is fairly easy

Floating Point Number representation


* The location of the fractional point is not fixed to a certain location
* The range of the representable numbers is wide
F = EM
mn ekek-1 ... e0 mn-1mn-2 m0 . m-1 m-m
sign exponent
mantissa
- Mantissa
Signed fixed point number, either an integer or a fractional number
- Exponent
Designates the position of the radix point

Decimal Value
V(F) = V(M) * RV(E)

M: Mantissa
E: Exponent
R: Radix

Floating Point Number Representation


Example

sign
0

.1234567
mantissa
==> +.1234567 x 10+04

sign
0
04
exponent

Note:
In Floating Point Number representation, only Mantissa(M) and
Exponent(E) are explicitly represented. The Radix(R) and the position
of the Radix Point are implied.
Example
A binary number +1001.11 in 16-bit floating point number representation
(6-bit exponent and 10-bit fractional mantissa)
0
or

Sign

0 00100 100111000
Exponent

Mantissa

0 00101 010011100

Normal Form
- There are many different floating point number representations of
the same number
Need for a unified representation in a given computer
- the most significant position of the mantissa contains a non-zero digit

IEEE-754 Floating Point Formats

Biased Exponent
All negative exponent are represented with a +ve
Quantity by adding a bias value
Single Precision
Bias Value = +127
Double Precision
Bias Value = +1023
Advantage: Exponents contain only +ve value.
Simple to compare the magnitudes.

IEEE-754 Examples

IEEE-754 Conversion Example


Represent -12.62510 in single precision IEEE-754 format.
Step #1: Convert to target base. -12.62510 = -1100.1012
Step #2: Normalize. -1100.1012 = -.11001012 24
Step #3: Fill in bit fields. Sign is negative, so sign bit is 1.
Exponent is represented as the
unsigned integer 4 + 127 = 131.
final bit pattern is:
1 1000 0011 . 11001 0100 0000 0000 0000 00

Character Representation ASCII


ASCII (American Standard Code for Information Interchange) Code
MSB (3 bits)

LSB
(4 bits)

0
1
2
3
4
5
6
7
8
9
A
B
C
D
E
F

NUL
SOH
STX
ETX
EOT
ENQ
ACK
BEL
BS
HT
LF
VT
FF
CR
SO
SI

DLE
DC1
DC2
DC3
DC4
NAK
SYN
ETB
CAN
EM
SUB
ESC
FS
GS
RS
US

SP
!

#
$
%
&

(
)
*
+
,
.
/

0
1
2
3
4
5
6
7
8
9
:
;
<
=
>
?

@
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O

P
Q
R
S
T
U
V
W
X
Y
Z
[
\
]
m
n

a
b
c
d
e
f
g
h
I
j
k
l
m
n
o

7
P
q
r
s
t
u
v
w
x
y
z
{
|
}
~
DEL

Control Character Representation


(ASCII)
NUL
SOH
STX
ETX
EOT
ENQ
ACK
BEL
BS
HT
LF
VT
FF
CR
SO
SI
DLE

Null
Start of Heading (CC)
Start of Text (CC)
End of Text (CC)
End of Transmission (CC)
Enquiry (CC)
Acknowledge (CC)
Bell
Backspace (FE)
Horizontal Tab. (FE)
Line Feed (FE)
Vertical Tab. (FE)
Form Feed (FE)
Carriage Return (FE)
Shift Out
Shift In
Data Link Escape (CC)

DC1
DC2
DC3
DC4
NAK
SYN
ETB
CAN
EM
SUB
ESC
FS
GS
RS
US
DEL

Device Control 1
Device Control 2
Device Control 3
Device Control 4
Negative Acknowledge (CC)
Synchronous Idle (CC)
End of Transmission Block (CC)
Cancel
End of Medium
Substitute
Escape
File Separator (IS)
Group Separator (IS)
Record Separator (IS)
Unit Separator (IS)
Delete

(CC) Communication Control


(FE) Format Effector
(IS) Information Separator

The EBCDIC character code,


shown with hexadecimal indices

The EBCDIC control character


representation

Represent 178.1875 and 0.0625 in single


precision and double precision.
Represent -59 in sign magnitude, 1s
complement and 2s complement form.

You might also like