Introduction To Computer Science 3
Introduction To Computer Science 3
Data
Storage
3.1
Foundations of Computer Science Cengage Learning
Outline
3.2
3-1 INTRODUCTION
3.4
Figure 3.3 Storage of different data types
3.5
Data compression
•To occupy less memory space, data is normally compressed
before being stored in the computer.
•Data compression is a very broad and involved subject, so
we have dedicated the whole of Chapter 15 to the subject.
i
Data compression is discussed in Chapter 15.
3.6
Error detection and correction
•Detection and correction of errors during transmission or
storage.
•We discuss this issue briefly in Appendix H.
i
Error detection and correction is discussed
in Appendix H.
3.7
3-2 STORING NUMBERS
3.8
Storing integers
•134 and −125 are integers, whereas 134.23 and −0.235 are
not.
•The decimal point is fixed: the decimal point is to the right
of the least significant (rightmost) bit.
3.9
Figure 3.4 Fixed point representation of integers
i
An integer is normally stored in memory using
fixed-point representation.
3.10
Unsigned representation
•unsigned integer: an integer that can never be negative
•Ranges between 0 and positive infinity
3.11
Example 3.1
Store 7 in an 8-bit memory location using unsigned
representation.
Solution
•Change the integer to binary, (111)2.
•Add five 0s to make a total of eight bits, (00000111)2.
•The integer is stored in the memory location.
• ***The subscript is not stored in the computer.
3.12
Example 3.2
Store 258 in a 16-bit memory location.
Solution
First change the integer to binary (100000010)2.
•First
•Add seven 0s to make a total of sixteen bits,
(0000000100000010)2.
•The integer is stored in the memory location.
3.13
Example 3.3
What is returned from an output device when it retrieves the bit
string 00101011 stored in memory as an unsigned integer?
Solution
Using the procedure shown in Chapter 2, the binary integer is
converted to the unsigned integer 43.
3.14
Figure 3.5 shows what happens if we try to store an integer
that is larger than 24 − 1 = 15 in a memory location that can
only hold four bits.
i
In sign-and-magnitude representation, the leftmost
bit defines the sign of the integer. If it is 0, the integer
is positive. If it is 1, the integer is negative.
3.16
Example 3.4
Store +28 in an 8-bit memory location using sign-and-magnitude
representation.
Solution
The integer is changed to 7-bit binary. The leftmost bit is set to 0.
The 8-bit number is stored.
3.17
Example 3.5
Store 28 in an 8-bit memory location using sign-and-magnitude
representation.
Solution
The integer is changed to 7-bit binary. The leftmost bit is set to 1.
The 8-bit number is stored.
3.18
Example 3.6
Retrieve the integer that is stored as 01001101 in sign-and-
magnitude representation.
Solution
Since the leftmost bit is 0, the sign is positive. The rest of the bits
(1001101) are changed to decimal as 77. After adding the sign,
the integer is +77.
3.19
Example 3.7
Retrieve the integer that is stored as 10100001 in sign-and-
magnitude representation.
Solution
Since the leftmost bit is 1, the sign is negative. The rest of the
bits (0100001) are changed to decimal as 33. After adding the
sign, the integer is −33.
3.20
Figure 3.7 shows both positive and negative overflow when
storing an integer in sign-and-magnitude representation
using a 4-bit memory location.
3.21
Figure 3.7 Overflow in sign-and-magnitude representation
Two’s complement representation
•Almost all computers use two’s complement representation
to store a signed integer in an n-bit memory location.
3.22
Figure 3.8 Two’s complement representation
i
In two’s complement representation, the leftmost bit
defines the sign of the integer. If it is 0, the integer is
positive. If it is 1, the integer is negative.
3.23
One’s Complementing
one’s complementing: simply reverses (flips) each bit. A 0-
bit is changed to a 1-bit, a 1-bit is changed to a 0-bit.
Example 3.8
The following shows how we take the one’s complement of the
integer 00110110.
3.24
Example 3.9
The following shows that we get the original integer if we apply
the one’s complement operations twice.
3.25
Two’s Complementing
two’s complementing: done in two steps.
•First, we copy bits from the right until a 1 is copied
•Then, we flip the rest of the bits.
Example 3.10
The following shows how we take the two’s complement of the
integer 00110100.
3.26
Example 3.11
The following shows that we always get the original integer if we
apply the two’s complement operation twice.
i
An alternative way to take the two’s complement of
an integer is to first take the one’s complement and
then add 1 to the result.
3.27
Example 3.12
Store the integer 28 in an 8-bit memory location using two’s
complement representation.
Solution
The integer is positive (no sign means positive), so after decimal
to binary transformation no more action is needed. Note that five
extra 0s are added to the left of the integer to make it eight bits.
3.28
Example 3.13
Store −28 in an 8-bit memory location using two’s complement
representation.
Solution
The integer is negative, so after changing to binary, the computer
applies the two’s complement operation on the integer.
3.29
Example 3.14
Retrieve the integer that is stored as 00001101 in memory in
two’s complement format.
Solution
The leftmost bit is 0, so the sign is positive. The integer is
changed to decimal and the sign is added.
3.30
Example 3.15
Retrieve the integer that is stored as 11100110 in memory using
two’s complement format.
Solution
The leftmost bit is 1, so the integer is negative. The integer needs
to be two’s complemented before changing to decimal.
3.31
i
There is only one zero in two’s complement notation.
3.32
Figure 3.9 Overflow in two’s complement representation
Comparison
3.33
Storing reals
•A real is a number with an integral part and a fractional
part.
•23.7 is a real number—the integral part is 27 and the
fractional part is 7/10.
•Although a fixed-point representation can be used to
represent a real number, the result may not be accurate or it
may not have the required precision.
i
Real numbers with very large integral parts or very
small fractional parts should not be stored in fixed-
point representation.
3.34
Example 3.16
•A fixed-point representation with 2 digits at the right of the
decimal point and 14 digits at the left of the decimal point, for a
total of 16 digits.
•Represent a decimal number such as 1.00234: the system stores
the number as 1.00.
•The precision is lost!
Example 3.17
•A fixed-point representation with 6 digits to the right of the
decimal point and 10 digits for the left of the decimal point, for a
total of 16 digits.
• Represent a decimal number such as 236154302345.00: the
system stores the number as 6154302345.00
• The integral part is much smaller than it should be.
3.35
Floating-point representation
The solution for maintaining accuracy or precision is to use
floating-point representation.
i
A floating point representation of a number is made up of
three parts: a sign, a shifter and a fixed-point number.
3.36
Example 3.18
The following shows the decimal number
7,452,000,000,000,000,000,000.00
The three sections are the sign (+), the shifter (21) and the fixed-
point part (7.425). Note that the shifter is the exponent.
3.37
Example 3.19
Show the number
−0.0000000000000232
in scientific notation (floating-point representation).
Solution
We use the same approach as in the previous example—we move
the decimal point after the digit 2, as shown below:
The three sections are the sign (), the shifter (14) and the
fixed-point part (2.32). Note that the shifter is the exponent.
3.38
Example 3.20
Show the number
(101001000000000000000000000000000.00)2
in floating-point representation.
Solution
We use the same idea, keeping only one digit to the left of the
decimal point.
3.39
Example 3.21
Show the number
−(0.00000000000000000000000101)2
in floating-point representation.
Solution
We use the same idea, keeping only one digit to the left of the
decimal point.
3.40
Normalization
•To make the fixed part of the representation uniform, only
one non-zero digit on the left of the decimal point is used.
•This is called normalization.
•In the decimal system this digit can be 1 to 9, while in the
binary system it can only be 1. In the following, d is a non-
zero digit, x is a digit, and y is either 0 or 1.
3.41
i
Note that the point and the bit 1 to the left of the
fixed-point section are not stored—they are implicit.
i
The mantissa is a fractional part that, together with
the sign, is treated like an integer stored in sign-and-
magnitude representation.
3.42
Excess System
•The exponent: shows how many bits the decimal point should be
moved to the left or right, is a signed number.
•In the Excess system, both positive and negative integers are
stored as unsigned integers.
•To represent a positive or negative integer, a positive integer
(called a bias) is added to each number to shift them uniformly to
the non-negative side.
•The value of this bias is 2m−1 − 1, where m is the size of the
memory location to store the exponent.
3.43
Example 3.22
3.46
Example 3.23
Show the Excess_127 (single precision) representation of the
decimal number5.75.
Solution
a. The sign is positive, so S = 0.
b. Decimal to binary transformation: 5.75 = (101.11)2.
c. Normalization: (101.11)2 = (1. 1011)2 × 22. (wrong!)
d. E = 2 + 127 = 129 = (10000001)2, M = 1011. We need to add
nineteen zeros at the right of M to make it 23 bits.
e. The presentation is shown below:
Solution
a. S = 1 (the number is negative).
b. Decimal to binary transformation: 0.0234375 = (0.0000011)2.
c. Normalization: (0.0000011)2 = (1.1)2 × 2−6.
d. E = –6 + 127 = 121 = (01111001)2 and M = (1)2.
e. Representation:
Solution
a. The first bit represents S, the next eight bits, E and the
remaining 23 bits, M.
Storing Zero
•A real number with an integral part and the fractional part set to
zero, that is, 0.0, cannot be stored using the steps discussed
above.
•To handle this special case, it is agreed that in this case the sign,
exponent and the mantissa are set to 0s.
3.51
3-3 STORING TEXT
•English language:
•26 symbols (A, B, C,…, Z) to represent uppercase
letters
•26 symbols (a, b, c, …, z) to represent lowercase letters
•9 symbols (0, 1, 2, …, 9) to represent numeric
characters
•symbols (., ?, :, ; , …, !) to represent punctuation.
•Other symbols such as blank, newline, and tab are used
for text alignment and readability.
3.52
•We can represent each symbol with a bit pattern.
•Text such as “CATS”, which is made up from four symbols, can
be represented as four n-bit patterns, each pattern defining a
single symbol (Figure 3.14).
3.53
3.54
Codes
ASCII
Unicode
Other Codes
i
See Appendix A
3.55
3-4 STORING AUDIO
3.57
Sampling
•If we cannot record all the values of a an audio signal over
an interval, we can record some of them.
•Sampling: to select only a finite number of points on the
analog signal, measure their values, and record them.
3.59
Encoding
•Encoded as bit patterns.
3.62
Resolution
•How many pixels we should record for each square or linear
inch.
•The scanning rate in image processing : resolution.
•If the resolution is sufficiently high, the human eye cannot
recognize the discontinuity in reproduced images.
Color depth
•color depth : The number of bits used to represent a pixel
3.63
True-Color
One of the techniques used to encode a pixel is called True-
Color, which uses 24 bits to encode a pixel.
3.64
Indexed color
The indexed color—or palette color—scheme uses only a
portion of these colors.
3.66
Standards for image encoding
•Several de facto standards for image encoding are in use.
3.67
Vector graphics
•Raster graphics has two disadvantages: the file size is big
and rescaling is troublesome.
•To enlarge a raster graphics image means enlarging the
pixels, so the image looks ragged when it is enlarged.
•The vector graphic image does not store the bit patterns for
each pixel.
•An image is decomposed into a combination of geometrical
shapes such as lines, squares or circles.
•For example, consider a circle of radius r:
i
See Chapter 15 for video compression.
3.69