lec02-numrep2

Download Report

Transcript lec02-numrep2

Data Representation
Lecture 21
5.3.2001.
13/2/2001
Sudeshna sarkar, CSE, IIT
Kharagpur
1
1001011101101

5
 1001001100101

12ed

13/2/2001
Sudeshna sarkar, CSE, IIT
Kharagpur
2
Signed Integer Representation

Many of the numerical data items that
are used in a program are signed
(positive or negative).


Question:: How to represent sign?
Three possible approaches:



13/2/2001
Sign-magnitude representation
One’s complement representation
Two’s complement representation
Sudeshna sarkar, CSE, IIT
Kharagpur
3
Sign-magnitude Representation

For an n-bit number representation

The most significant bit (MSB) indicates sign
 0  positive
 1  negative

The remaining n-1 bits represent magnitude.
b1 b0
bn-1bn-2
Sign
13/2/2001
Magnitude
Sudeshna sarkar, CSE, IIT
Kharagpur
4
Signed Magnitude scheme

Range of numbers that can be
represented:
Maximum :: + (2n-1 – 1)
Minimum ::  (2n-1 – 1)

A problem:
Two different representations of zero.


13/2/2001
+0  0 000….0
-0  1 000….0
Sudeshna sarkar, CSE, IIT
Kharagpur
5
One’s Complement Representation

Basic idea:



Positive numbers are represented exactly as in signmagnitude form.
Negative numbers are represented in 1’s complement
form.
How to compute the 1’s complement of a number?


Complement every bit of the number (10 and 01).
MSB will indicate the sign of the number.
 0  positive
 1  negative
13/2/2001
Sudeshna sarkar, CSE, IIT
Kharagpur
6
n=4








0000
0001
0010
0011
0100
0101
0110
0111








+0
+1
+2
+3
+4
+5
+6
+7








1000
1001
1010
1011
1100
1101
1110
1111








-7
-6
-5
-4
-3
-2
-1
-0
To find the representation of, say, -4, first note that
+4 = 0100
-4 = 1’s complement of 0100 = 1011
13/2/2001
Sudeshna sarkar, CSE, IIT
Kharagpur
7



Range of numbers that can be represented:
Maximum :: + (2n-1 – 1)
Minimum ::  (2n-1 – 1)
A problem:
Two different representations of zero.
 +0
 0 000….0
 -0
 1 111….1
Advantage of 1’s complement representation
 Subtraction can be done using addition.
 Leads to substantial saving in circuitry.
13/2/2001
Sudeshna sarkar, CSE, IIT
Kharagpur
8
Two’s Complement Representation


Basic idea:
 Positive numbers are represented exactly as in
sign-magnitude form.
 Negative numbers are represented in 2’s
complement form.
How to compute the 2’s complement of a number?
 Complement every bit of the number (10 and
01), and then add one to the resulting number.
 MSB will indicate the sign of the number.
 0  positive
 1  negative
13/2/2001
Sudeshna sarkar, CSE, IIT
Kharagpur
9

In C



13/2/2001
short int
 16 bits
 + (215-1) to -215
int
 32 bits
 + (231-1) to -231
long int
 64 bits
 + (263-1) to -263
Sudeshna sarkar, CSE, IIT
Kharagpur
10
Two’s complement

Consider 8 bit numbers. In this system
if the value of the MSB is:


13/2/2001
zero : then the rest of the seven bits
represent normal binary numbers (0 to
127).
one : then this represents -128 which is
added to the total of the other seven bits
that represent a normal binary number.
Sudeshna sarkar, CSE, IIT
Kharagpur
11
Example : n=8





00000001
00000101
10000001
10000101
11011111





+1
+5
-127
-123
-33
To find the representation of, say, -4, first note that
+4 = 00000100
-4 = 2’s complement of 00000100
= 11111011+1 = 11111100
Sudeshna sarkar, CSE, IIT
13/2/2001
Kharagpur
12

Range of numbers that can be represented:
Maximum :: + (2n-1 – 1)
Minimum ::  2n-1

Advantage:




Unique representation of zero.
Subtraction can be done using addition.
Leads to substantial saving in circuitry.
Most computers today use the 2’s complement
representation for storing negative numbers.
13/2/2001
Sudeshna sarkar, CSE, IIT
Kharagpur
13
Two’s complement arithmetic


Two’s complement is very important
when we wish to perform a subtraction
between two binary numbers.
Adding one binary number to the 2’s
complement of another binary number
is equivalent to a subtraction operation.
13/2/2001
Sudeshna sarkar, CSE, IIT
Kharagpur
14
Negative numbers in 2’s complement


1. Flip all the binary digits within the
corresponding positive number (1’s
complement).
2. Add 1 to the result.
 13: 00001101
 1’s complement : 11110010
 2’s complement : 11110011
This represents -13
(-128+64+32+16+0+0+2+1)
Sudeshna sarkar, CSE, IIT
13/2/2001
Kharagpur
15
2’s complement arithmetic




Example 1: 18-11 = ?
18 is represented as 00010010
11 is represented as 00001011
 1’s complement of 11 is 11110100
 2’s complement of 11 is 11110101
Add 18 to 2’s complement of 11
00010010
+ 11110101
00000111 is binary
---------------for
7.
00000111 (with a carry of 1
Sudeshna sarkar, CSE, IIT
which
is
ignored)
13/2/2001
Kharagpur
16
2’s complement arithmetic



Example 2: 7 - 9 = ?
7 is represented as 00000111
9 is represented as 00001001
 1’s complement of 9 is 11110110
 2’s complement of 9 is 11110111
 Add 7 to 2’s complement of 9
00000111
+ 11110111
00000111 is binary
---------------for
-2.
11111110 (with a carry of 0
Sudeshna sarkar, CSE, IIT
which
is
ignored)
13/2/2001
Kharagpur
17
Overflow

Overflow condition : carry in and carry
out from MSB are different.
(64) 01000000
( 4) 00000100
-------------(68) 01000100
carry (out)(in)
0 0
13/2/2001
(64) 01000000
(96) 01100000
-------------(-96) 10100000
carry out in
0 1
Sudeshna sarkar, CSE, IIT
Kharagpur
differ:
overflow
18
Real Numbers:
floating point representation


1.5, 36.415 : how to represent these
numbers ?
In floating point representation, a
number is represented in two parts :
1. mantissa (M)
F = M x BE
 2. exponent (E)
B: exponent base
1903810 = 1.9038 x 104
0.0032510 = 3.25 x 10-3

13/2/2001
Sudeshna sarkar, CSE, IIT
Kharagpur
19
Floating point : binary numbers

All of the mantissa appears to the right
of the decimal (binary?) point.

1101.00112 becomes 0.110100112 x 24

0.0001012 becomes 0.1012 x 2-3
13/2/2001
Sudeshna sarkar, CSE, IIT
Kharagpur
20
Storage of floating point numbers

Two pieces of information need to be stored :
 1. mantissa : more bits for mantissa means
more precision

13/2/2001
2. exponent : more bits in the exponent
provides a greater range of numbers.
1. M represents a 2’s
complement fraction
M
E
1 > M > -1
24
8
2. E represents the
exponent in 2’s
Sudeshna sarkar, CSE, IIT complement
Kharagpur
21

In C:



4 bytes for single precision float
8 bytes for double
Different computer systems use
different data representations for
floating point numbers.
13/2/2001
Sudeshna sarkar, CSE, IIT
Kharagpur
22
Example
Mantissa Decimal Exponent Decimal
eqv of M
eqv of E
Decimal
value
01000000 ½+0+0+0
+0+0+0
0000
(=1/2)
0001
21=2
01000000 ½+0+0+0
+0+0+0
0000
(=1/2)
1111
-8+4+2+1 ½ x 2-1
= -1
=
01101001 ½+1/4+0 0101
0000
+1/16+0
+0+1/128
13/2/2001
1
1/4
25 = 32
Sudeshna sarkar, CSE, IIT
Kharagpur
26.25
23
Range of values

4 bytes are used to represent a floating point
number.
Mantissa (3 bytes) and
 exponent (1 byte)
are in 2’s complement form.
What would be the smallest, greatest, and the smallest positive
fraction that could be handled by this system ?

13/2/2001
Sudeshna sarkar, CSE, IIT
Kharagpur
24



1. The largest single precision number is
01111111 11111111 11111111 01111111
2. The smallest number (most -ve) would be
10000000 00000000 00000000 01111111
3. The smallest fraction would be:
00000000 00000000 00000001 10000000
13/2/2001
Sudeshna sarkar, CSE, IIT
Kharagpur
25
Representation of Characters

Many applications have to deal with non-numerical
data.



Characters and strings.
There must be a standard mechanism to represent
alphanumeric and other characters in memory.
Two standards in use:


Extended Binary Coded Decimal Interchange Code
(EBCDIC) -- Used in older IBM machines
American Standard Code for Information Interchange
(ASCII)
 Most widely used today
13/2/2001
Sudeshna sarkar, CSE, IIT
Kharagpur
26
ASCII code


Each character (letters, numerals,
punctuation marks, control characters and
other symbols) is assigned a numeric code (7
or 8 bits).
The binary encoding of the characters follow
a regular ordering.


13/2/2001
Digits are ordered consecutively in their proper
numerical sequence (0 to 9).
Letters (uppercase and lowercase) are arranged
consecutively in their proper alphabetic order.
Sudeshna sarkar, CSE, IIT
Kharagpur
27
Some Common ASCII Codes
‘A’ :: 41 (H) 65 (D)
‘B’ :: 42 (H) 66 (D)
………..
‘Z’ :: 5A (H) 90 (D)
‘a’ :: 61 (H) 97 (D)
‘b’ :: 62 (H) 98 (D)
………..
‘z’ :: 7A (H) 122 (D)
13/2/2001
‘0’ :: 30 (H) 48 (D)
‘1’ :: 31 (H) 49 (D)
………..
‘9’ :: 39 (H) 57 (D)
‘(‘ ::
‘+’ ::
‘?’ ::
‘\n’ ::
‘\0’ ::
Sudeshna sarkar, CSE, IIT
Kharagpur
28 (H)
2B (H)
3F (H)
0A (H)
00 (H)
40 (D)
43 (D)
63 (D)
10 (D)
00 (D)
28
Character Strings



Two ways of representing a sequence of characters in
memory.
 The first location contains the number of characters in
the string, followed by the actual characters.
 The characters follow one another, and is terminated
by a special delimiter.
In C, the second approach is used.
 The ‘\0’ character is used as the string delimiter.
Example: “Hello”
13/2/2001

H
Sudeshna sarkar, CSE, IIT
Kharagpur
e l
l
o “0”
29