Title: Data Representation
1Data Representation
2- 1001011101101
- 5
- 1001001100101
- 12ed
3Signed 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
- Sign-magnitude representation
- Ones complement representation
- Twos complement representation
4Sign-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.
b0
bn-1
bn-2
b1
Sign
Magnitude
5Signed Magnitude scheme
- 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 000.0
6Ones Complement Representation
- Basic idea
- Positive numbers are represented exactly as in
sign-magnitude form. - Negative numbers are represented in 1s
complement form. - How to compute the 1s 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
7n4
- 1000 ? -7
- 1001 ? -6
- 1010 ? -5
- 1011 ? -4
- 1100 ? -3
- 1101 ? -2
- 1110 ? -1
- 1111 ? -0
- 0000 ? 0
- 0001 ? 1
- 0010 ? 2
- 0011 ? 3
- 0100 ? 4
- 0101 ? 5
- 0110 ? 6
- 0111 ? 7
To find the representation of, say, -4, first
note that 4 0100 -4 1s
complement of 0100 1011
8- 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 1s complement representation
- Subtraction can be done using addition.
- Leads to substantial saving in circuitry.
9Twos Complement Representation
- Basic idea
- Positive numbers are represented exactly as in
sign-magnitude form. - Negative numbers are represented in 2s
complement form. - How to compute the 2s 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
10- In C
- short int
- 16 bits ? (215-1) to -215
- int
- 32 bits ? (231-1) to -231
- long int
- 64 bits ? (263-1) to -263
11Twos complement
- Consider 8 bit numbers. In this system if the
value of the MSB is - 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.
12Example n8
- 00000001 ? 1
- 00000101 ? 5
- 10000001 ? -127
- 10000101 ? -123
- 11011111 ? -33
To find the representation of, say, -4, first
note that 4 00000100 -4
2s complement of 00000100
111110111 11111100
13- 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 2s complement
representation for storing negative numbers.
14Twos complement arithmetic
- Twos complement is very important when we wish
to perform a subtraction between two binary
numbers. - Adding one binary number to the 2s complement of
another binary number is equivalent to a
subtraction operation.
15Negative numbers in 2s complement
- 1. Flip all the binary digits within the
corresponding positive number (1s complement). - 2. Add 1 to the result.
- 13 00001101
- 1s complement 11110010
- 2s complement 11110011
- This represents -13
- (-1286432160021)
162s complement arithmetic
- Example 1 18-11 ?
- 18 is represented as 00010010
- 11 is represented as 00001011
- 1s complement of 11 is 11110100
- 2s complement of 11 is 11110101
- Add 18 to 2s complement of 11
00010010 11110101 ----------------
00000111 (with a carry of 1
which is ignored)
00000111 is binary for 7.
172s complement arithmetic
- Example 2 7 - 9 ?
- 7 is represented as 00000111
- 9 is represented as 00001001
- 1s complement of 9 is 11110110
- 2s complement of 9 is 11110111
- Add 7 to 2s complement of 9
00000111 11110111 ----------------
11111110 (with a carry of 0
which is ignored)
00000111 is binary for -2.
18Overflow
- Overflow condition carry in and carry out from
MSB are different.
(64) 01000000 ( 4) 00000100
-------------- (68) 01000100
(64) 01000000 (96) 01100000
-------------- (-96) 10100000
carry (out)(in) 0 0
carry out in 0 1
differ overflow
19Real Numbersfloating 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
20Floating 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
21Storage of floating point numbers
- Two pieces of information need to be stored
- 1. mantissa more bits for mantissa means more
precision - 2. exponent more bits in the exponent provides
a greater range of numbers.
1. M represents a 2s complement fraction 1
gt M gt -1 2. E represents the exponent in 2s
complement
M
E
24
8
22- In C
- 4 bytes for single precision float
- 8 bytes for double
- Different computer systems use different data
representations for floating point numbers.
23Example
24Range of values
- 4 bytes are used to represent a floating point
number. - Mantissa (3 bytes) and
- exponent (1 byte)
- are in 2s complement form.
- What would be the smallest, greatest, and the
smallest positive fraction that could be handled
by this system ?
25- 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
26Representation 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
27ASCII 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. - Digits are ordered consecutively in their proper
numerical sequence (0 to 9). - Letters (uppercase and lowercase) are arranged
consecutively in their proper alphabetic order.
28Some Common ASCII Codes
- 0 30 (H) 48 (D)
- 1 31 (H) 49 (D)
- ..
- 9 39 (H) 57 (D)
- ( 28 (H) 40 (D)
- 2B (H) 43 (D)
- ? 3F (H) 63 (D)
- \n 0A (H) 10 (D)
- \0 00 (H) 00 (D)
- 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)
29Character 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 ?
e
H
l
o
0
l