Title: Data Representation and Storage
1Data Representation and Storage
2Integer Binary Numbers
- Integers are whole numbers (i.e. have no
fractional part) - Ex 10, 6, 1, -33
- Decimal representation Binary representation Store
d in memory as - (base 10) (base 2)
- 0 0 0000 0000 0000 0000
- 1 1 0000 0000 0000 0001
- 2 10 0000 0000 0000 0010
- 3 11 0000 0000 0000 0011
- 4 100 0000 0000 0000 0100
- 5 101 0000 0000 0000 0101
- 6 110 0000 0000 0000 0110
- 7 111 0000 0000 0000 0111
- 8 1000 0000 0000 0000 1000
3Integer Binary Numbers
- Binary integer numbers can be interpreted very
similar to decimal integer numbers - The decimal (base 10) integer number 7341 can be
interpreted as - 7 ? 103 3 ? 102 4 ? 101 1 ? 100 7341
- The binary (base 2) integer number 1101 can be
interpreted as - 1 ? 23 1 ? 22 0 ? 21 1 ? 20 13
4Unsigned Integers
- With a 16 bit word we can represent numbers from
- 0000 0000 0000 00002 to 1111 1111 1111 11112
- which corresponds to 0 to 65536.
- Note that we have assumed that the numbers are
greater than or equal to zero (i.e. that there
are no negative numbers). This data type is known
as unsigned integers. - Variable type Memory size in bits Range
- unsigned short int 16 0 to (216 - 1)
- unsigned int 32 0 to (232 - 1)
- unsigned long 32 0 to (232 - 1)
51s Complement Representation
- So how do we represent negative numbers???
- Conceptually, the easiest way to represent
negative numbers is to use the most significant
bit (bit furthest to the left) as a sign bit.
This is known as the 1s complement
representation. - Decimal representation Binary representation Store
d in memory as - (base 10) (base 2)
- -3 11 1000 0000 0000 0011
- -2 10 1000 0000 0000 0010
- -1 1 1000 0000 0000 0001
- 0 0 0000 0000 0000 0000
- 1 1 0000 0000 0000 0001
- 2 10 0000 0000 0000 0010
- 3 11 0000 0000 0000 0011
62s Complement Representation
- Despite, its conceptual simplicity the 1s
complement representation is not widely used.
Instead, the 2s complement representation is
often used. To find the 2s complement
representation, find the positive binary
representation of the number, invert that
representation and add 1. - Example Find the 2s complement representation
of -468 - 468 0000 0001 1101 01002
- Invert 1111 1110 0010 10112
- Add 1 1
- 2s complement 1111 1110 0010 11002
- representation of 468
72s Complement Representation
- The utility of the 2s complement representation
can be seen when we try to add -1 and 1. - 1s complement 2s complement
- 1000 0000 0000 0001 1111 1111 1111 1111
- 0000 0000 0000 0001 0000 0000 0000
0001 - 1000 0000 0000 0010 1 0000 0000
0000 0000 - is not correct correct if we simply ignore the
overflow bit
82s Complement Representation
- Some arithmetic logic units (ALUs) can only
perform addition. In these cases subtraction can
be performed by using the ALU to take the 2s
complement of the second operand (many ALUs can
perform a bit inversion and increment) and then
adding the result with the first operand. - X - Y X (-Y)
9Signed Integers
- With a 16 bit word (using 2s complement
representation) we can represent positive numbers
from - 0000 0000 0000 00002 to 0111 1111 1111 11112
- which corresponds to 0 to 32767.
- And negative numbers from
- 1111 1111 1111 11112 to 1000 0000 0000 00002
- which corresponds to -1 to -32768.
- Variable type Memory size in bits Range
- short int 16 -215 to (215 - 1)
- int 32 -231 to (231 - 1)
- long 32 -231 to (231 - 1)
10Binary Representation of Real Numbers
11Real Numbers
- Real numbers are the set of rational and
irrational numbers. These numbers may include a
fraction component. Computers use a
floating-point representation for real numbers - mantissa ? baseexponent
- In decimal format we refer to this as scientific
notation - For example 23.25 2.325 ? 101
- The same idea applies for binary numbers (base 2)
- 1.0111012 ? 24
- 1 ? 20 0 ? 2-1 1 ? 2-2 1 ? 2-3 1 ? 2-4
0 ? 2-5 1 ? 2-6 ? 24 - 1.453125 ? 24
- 23.25
12IEEE Floating Point Representation
- The IEEE floating point standards are widely
used. - sign bit exponent mantissa
- Type exp bits mantissa
bits approximate range significant digits - float 8 11 -1038 to 1038 7
- double 11 53 -10308 to 10308 16
- long double 15 64 very large! 20
0
011 1111 0
100 0000 0000 0000 0000 0000
13IEEE Floating Point Representation
- The binary mantissa should always begin with a 1.
- 0.00101 ? 22 1.01 ? 25
- IEEE standards use this knowledge by having an
implied 1 at the beginning of the mantissa. - The sign bit is used to indicate whether to
entire number is positive (sign-bit 0) or
negative (sign-bit 1). - The exponents values are stored with a bias equal
to half their possible value. For a 32-bit
floating point number the exponent value is
represented by 8-bits. The exponent therefore has
a maximum value of 255 (1111 11112) so its bias
it 127.
14IEEE Floating Point Representation
- Value (Sign-bit S) (1.Mantissa bits)2 ?
2(exponent value bias) - Example Take the following 32-bit floating point
number - S EEE EEEE E MMM MMMM MMMM MMMM MMMM MMMM
- 0 011 1111 0 100 0000 0000 0000 0000 0000
- The value of this floating point number can be
solved as - The sign bit is S 0 so the sign of the number
is positive - The exponent bits are 0111 11102 126
- Since the exponent is an 8-bit number the bias is
127 - The Mantissa bits are 1.100 0000 0000 0000 0000
00002 (recall we automatically append the 1. to
the front of the specified Mantissa bits). This
has a value of - 1 ? 20 1 ? 2-1 0 ? 2-2 0 ? 2-3 0 ?
2-23 1.5 - The value of the floating point number is
- () 1.5 ? 2(126 127) 0.75
15IEEE Floating Point Representation
- Examples
- S EEE EEEE E MMM MMMM MMMM MMMM MMMM MMMM
- 2 0 100 0000 0 000 0000 0000 0000 0000 0000
1 ? 2(128 - 127) - -2 1 100 0000 0 000 0000 0000 0000 0000 0000
-1 ? 2(128 - 127) - 4 0 100 0000 1 000 0000 0000 0000 0000 0000
1 ? 2(129 - 127) - 6 0 100 0000 0 100 0000 0000 0000 0000 0000
1.5 ? 2(128 - 127) - 1 0 011 1111 1 000 0000 0000 0000 0000 0000
1 ? 2(127 - 127) - .75 0 011 1111 0 100 0000 0000 0000 0000 0000
1.5 ? 2(126 - 127) - 0 0 000 0000 0 000 0000 0000 0000 0000 0000
special case
16Roundoff Errors
- Roundoff Error the mantissa only has a finite
length, which limits the number of significant
digits that can be stored - Example
- S EEE EEEE E MMM MMMM MMMM MMMM MMMM
MMMM - 0.1 1.6 ? 2(123 - 127) 0 011 1101 1 100
1100 1100 1100 1100 1101 - Since 1.6 is represented by a repeating binary
number 1.1001 1001 1001 . we need to truncate
the number since there are only 23 bits in the
mantissa. - 0.1 represented in data type double 0.1000 0000
0000 0000 0555 1331 2 - Arithmetic overflow too large of a number to
store - Arithmetic underflow too small of a number to
store
17Binary Representation of Characters
18ASCII code
- Characters use the type char which uses 8 bits of
memory. - The American Standard Code for Information
Interchange (ASCII) code is the most widely used
standard. - Extended Binary Coded Decimal Interchange Code
(EBCDIC) is another code, which is probably the
second most popular code. - The type char is used to represent
- Uppercase letters A through Z
- Lowercase letters a through z
- Digits 0 through 9
- Punctuation, such as colon (), comma (,),
period(.) - Special characters, such as percent sign (),
octothorp (), ampersand () - Formatting/communication characters, such as
carriage return, backspace, escape
19- Dec Hex Char Dec Hex
Char Dec Hex Char Dec Hex Char - -----------
------------ ------------ ------------ - 0 0 NUL (null) 32 20
64 40 _at_ 96 60 - 1 1 SOH (start of heading) 33 21 !
65 41 A 97 61 a - 2 2 STX (start of text) 34 22 "
66 42 B 98 62 b - 3 3 ETX (end of text) 35 23
67 43 C 99 63 c - 4 4 EOT (end of transmission) 36 24
68 44 D 100 64 d - 5 5 ENQ (enquiry) 37 25
69 45 E 101 65 e - 6 6 ACK (acknowledge) 38 26
70 46 F 102 66 f - 7 7 BEL (bell) 39 27 '
71 47 G 103 67 g - 8 8 BS (backspace) 40 28 (
72 48 H 104 68 h - 9 9 TAB (horizontal tab) 41 29 )
73 49 I 105 69 i - 10 A LF (NL line feed, new line) 42 2A
74 4A J 106 6A j - 11 B VT (vertical tab) 43 2B
75 4B K 107 6B k - 12 C FF (NP form feed, new page) 44 2C ,
76 4C L 108 6C l - 13 D CR (carriage return) 45 2D -
77 4D M 109 6D m - 14 E SO (shift out) 46 2E .
78 4E N 110 6E n - 15 F SI (shift in) 47 2F /
79 4F O 111 6F o - 16 10 DLE (data link escape) 48 30 0
80 50 P 112 70 p
20Problem 1 Integers
- Confirm the 16-bit 1s complement and 2s
complement representations of the following
numbers by converting them yourself. - Decimal Binary 1s complement Binary 2s
complement - 8 0000 0000 0000 1000 0000 0000 0000 1000
- 8636 0010 0001 1011 1100 0010 0001 1011 1100
- -2 1000 0000 0000 0010 1111 1111 1111 1110
- -403 1000 0001 1001 0011 1111 1110 0110 1101
21Problem 2 Integers
- Perform the following addition using the 16-bit
2s complement binary representations and convert
the result back into the decimal notation to
confirm the correctness of the result. - 8 8636
- 8 (-2)
- -403 8636
- -2 (-403)
22Problem 3 Floating Point
- Show that the binary representation for the
floating point numbers are equal to the
corresponding decimal values. - S EEE EEEE E MMM MMMM MMMM MMMM MMMM MMMM
- 0.5625 0 011 1111 0 001 0000 0000 0000 0000
0000 - 0.6445312500 0 011 1111 0 010 0101 0000 0000
0000 0000 - -0.1455078125 1 011 1110 0 001 0101 0000 0000
0000 0000 - 127.8544921875 0 100 0010 1 111 1111 1011 0101
1000 0000
23Problem 4 Characters
Determine what the following code will output to
the screen.
- include ltiostreamgt
- using namespace std
- int main()
-
- char char1 'A'
- char char2 'Z'
- char char3
-
- cout ltlt "char1 " ltlt char1 ltlt endl
- cout ltlt "char2 " ltlt char2 ltlt endl
- char3 char1 10
- cout ltlt "char3 " ltlt char3 ltlt endl
-
- char3 char2 - 5
- cout ltlt "char3 " ltlt char3 ltlt endl