0% found this document useful (0 votes)
3 views

COS1521-foundations_of_computer_science_-chapter_3

Uploaded by

Sootha Deea
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

COS1521-foundations_of_computer_science_-chapter_3

Uploaded by

Sootha Deea
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 78

Foundations of Computer Science

Second Edition
BEHROUZ FOROUZAN
FIROUZ MOSHARRAF

Chapter 3
Data Storage

1
Outlines

z Data Types
z Storing numbers
z Storing text
z Storing audio
z Storing images
z Storing video

2 CSIM@PU
Objectives

After studying this chapter, the student should


understand
z Five different data types used in a computer.
z How different data is stored inside a computer.
z How integers are stored in a computer.
z How reals are stored in a computer.
z How text is stored in a computer.
z How audio is stored in a computer.
z How images are stored in a computer.
3 z How video is stored in a computer. CSIM@PU
3.1 Data Types

4
Introduction

• Different types of data (Fig. 3.1)

The computer industry uses the term “multimedia” to


define information that contains numbers,
text, images, audio and video.
5 CSIM@PU
Data inside the computer

z All data types are transformed into a uniform


representation when they are stored in a
computer and transformed back to their
original form when retrieved. This universal
representation is called a bit pattern.

6
7 Figure 3.3 Storage of different data types
Data compression &
Error detection and correction

z Data compression
– To occupy less memory space, data is normally
compressed before being stored in the computer.
– Details in Chapter 15.
z Error detection and correction
– Another issue related to data is the detection and
correction of errors during transmission or
storage.
– Briefly in Appendix H.

8
3.2 Storing Numbers

9
Overview

z A number is changed to the binary system


before being stored in the computer memory,
as described in Chapter 2.
z Problems:
– How to store the sign of the number.
– How to show the decimal point.

10 CSIM@PU
Storing Integers

z Integers (without a fractional part), for examples:


134 and −125 are integers, but 134.23 and −0.235 are not
z An integer can be thought of as a number in which the
position of the decimal point is fixed
z Fixed-point representation is used to store an integer
(Fig. 3.4). The decimal point is assumed but not stored

An integer is normally stored in memory using


11 fixed-point representation.
Unsigned representation (1)

z An unsigned integer is an integer that can


never be negative. Its range is between 0
and positive infinity.
z An input device stores an unsigned integer
using the following steps:
– The integer is changed to binary.
– If the number of bits is less than n, 0s are added
to the left.

12
Unsigned representation (2)

z Example 3.1 Store 7 in an 8-bit memory


location using unsigned representation.
– First 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,


but the subscript is not stored in the computer
13
Unsigned representation (3)

z Example 3.2 store 258 in a 16-bit memory location.


– First change the integer to binary (100000010)2.
– Add seven 0s to make a total of sixteen bits,
(0000000100000010)2.

z Example 3.3
– What is returned when retrieving the bit string 00101011
stored in memory as an unsigned integer?
– The binary integer is converted to the unsigned integer 43.
14
Unsigned representation (4)

z Figure 3.5 stores an integer that is larger


than 24 − 1 = 15 in a memory location that
can only hold four bits. (Overflow)

15
Sign-and-magnitude
representation (1)

z The range for unsigned integers (0 to 2n − 1)


is divided into two equal sub-ranges.
– The first half represents positive integers,
– the second half, negative integers.

The leftmost bit defines the sign. If it is 0, the integer


is positive. If it is 1, the integer is negative.
16
Sign-and-magnitude
representation (2)

z Example 3.4 stores +28 in an 8-bit memory


location
z Solution
– Changing the integer to 7-bit binary
– The leftmost bit is set to 0
– The 8-bit number is stored.

17
Sign-and-magnitude
representation (3)

z Example 3.5 stores -28 in an 8-bit memory


location
z Solution
– Changing the integer to 7-bit binary.
– The leftmost bit is set to 1.
– The 8-bit number is stored.

18
Sign-and-magnitude
representation (4)

z Example 3.6 retrieves the integer that is stored as


01001101 in sign-and-magnitude representation.
– Since the leftmost bit is 0, the sign is positive.
– The rest of the bits (1001101) are changed to decimal (77).
– After adding the sign, the integer is +77.

z Example 3.7 retrieves the integer that is stored as


10100001 in sign-and-magnitude representation.
– Since the leftmost bit is 1, the sign is negative.
– The rest of the bits (0100001) are changed to decimal (33).
– After adding the sign, the integer is −33.
19
Sign-and-magnitude
representation (5)

z Positive and negative overflow when storing an integer


20 using a 4-bit memory location.
Two’s complement representation (1)

z Almost all computers use 2’s complement


representation to store a signed integer in an n-
bit memory location.
– The available range for an unsigned integer of (0 to 2n
− 1) is divided into two equal sub-ranges.
– The first sub-range for nonnegative integers,
– The second half for negative integers.

21
Two’s complement representation (2)

z The bit patterns are assigned to negative and


nonnegative integers as shown in the Figure.

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.
22
One’s Complementing (1)

z Taking the one’s complement of an integer.


z The operation can be applied to any integer,
positive or negative.
z This operation simply reverses (flips) each
bit. A 0-bit is changed to a 1-bit, a 1-bit is
changed to a 0-bit.

23
One’s Complementing (2)

z Example 3.8 shows the one’s complement of the


integer 00110110.

z Example 3.9 shows that we get the original integer if


we apply the one’s complement operations twice.

24
Two’s Complementing (1)

z Taking the two’s complement of an integer in


binary.
z Two steps
– First, copying bits from the right until a 1
– Then, flipping the rest of the bits.
z Example 3.10 shows the two’s complement of
the integer 00110100.

25
Two’s Complementing (2)

z Example 3.11: we get the original integer if


applying the 2’s complement operation twice.

An alternative way to take the 2’s complement of an integer


is to first take the 1’s complement and then add 1 to the result.
26
Two’s Complementing (3)

z Example 3.12 stores the integer 28 in an 8-bit


memory location using 2’s complement
representation.

z Example 3.13 stores −28 in an 8-bit memory


location using 2’s complement representation.

27
z Example 3.14 retrieves the integer that is stored as
00001101 in memory in two’s complement format.

z Example 3.15 retrieves the integer stored as


11100110 in memory using two’s complement format.

28
Two’s Complementing (4)
Overflow

29 There is only one zero in two’s complement notation.


Comparison

30
Storing Reals (1)

z A real is a number with an integral part and a


fractional part.

z 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.
The next two examples explain why.

z Real numbers with very large integral parts or very


small fractional parts should not be stored in fixed-
point representation.
31
Storing Reals (2)

z Example 3.16 tries to represent a decimal number


such as 1.00234 to a total of sixteen digits.
– Two digits at the right of the decimal point
– Fourteen digits at the left of the decimal point
– The system stores the number as 1.00; The precision of a real
number in this system is lost
z Example 3.17 tries to represent a decimal number
such as 236154302345.00 to a total of sixteen digits.
– Six digits to the right of the decimal point
– Ten digits for the left of the decimal point,
– The system stores the number as 6154302345.00; The
32 accuracy of a real number in this system is lost
Floating-point representation (1)

z The solution for maintaining accuracy or


precision is to use floating-point representation.
z Three parts: a sign, a shifter and a fixed-point
number. (Figure 3.9)

33
Floating-point representation (2)

z Example 3.18 shows the decimal number in


floating-point representation.
7,452,000,000,000,000,000,000.00
z The three parts are
– The sign (+), the shifter (21), and the fixed-point
part (7.425).
– Note that the shifter is the exponent.

34
Floating-point representation (3)

z Example 3.19 shows the number in floating-


point representation.
−0.0000000000000232
z The three parts are
– The sign (-), the shifter (-14), and the fixed-point
part (2.32)

35
Floating-point representation (4)

z Examples (3.20 and 3.21) show the numbers in


floating-point representation.
– Keeping only one digit to the left of the decimal point.
Eg. 3.20: (101001000000000000000000000000000.00)2

Eg. 3.21: −(0.00000000000000000000000101)2

36
Normalization (1)

z Both the scientific method (for the decimal


system) and the floating-point method (for
the binary system) use only one non-zero
digit on the left of the decimal point.

z In the decimal system this digit can be 1 to 9,


while in the binary system it can only be 1.

37
Normalization (2)

z Note that the point and the bit 1 to the left of the
fixed-point section are not stored—they are implicit.
z The mantissa is a fractional part that, together with
the sign, is treated like an integer stored in sign-and-
magnitude representation.
38
Excess System (1)

z Exponent
– To show how many bits the decimal point should be moved to the
left or right.
– Being a signed number (使用Excess system)
z Excess System
– Positive and negative integers are stored as unsigned integers.
– A positive integer (called a bias) is added to each number to shift
them uniformly to the non-negative side.
z The value of this bias is 2m-1 − 1, where m is the size of the
memory location to store the exponent.
39
Excess System (2)

z Example 3.22 shows sixteen integers with 4-bit


allocation.
– By adding seven units to each integer, uniformly
translating all integers to the right.
– The new system is referred to as Excess-7, or biased
representation with biasing value of 7.

40
41
IEEE Standard

Figure 3.12 IEEE standards for floating-point representation


42
IEEE Specifications

43
Excess System Example (1)

z Example 3.23 show the Excess_127 (single precision)


representation of the decimal number 5.75.
Solution
• The sign is positive, so S = 0.
• Decimal to binary transformation: 5.75 = (101.11)2.
• Normalization: (101.11)2 = (1.1011)2 × 22.
• E = 2 + 127 = 129 = (10000001)2, M = 1011.
• We need to add nineteen zeros at the right of M to make it 23 bits.

The number is stored in the computer as


44 01000000110110000000000000000000
Excess System Example (2)

z Example 3.24 shows the Excess_127


representation of –161.875.
Solution:
• The sign is negative, so S = 1.
• Decimal to binary: 161.875= (10100001.111)2.
• Normalization: (10100001.111)2 = (1.0100001111)2 × 27.
• E = 7 + 127 = 134 = (10000110)2 and M = (0100001111)2.

The number is stored in the computer as


45 11000011010000111100000000000000
Excess System Example (3)

z Example 3.25 shows the Excess_127


representation of –0.0234375.
Solution
• S = 1 (the number is negative).
• Decimal to binary: 0.0234375 = (0.0000011)2.
• Normalization: (0.0000011)2 = (1.1)2 × 2−6.
• E = –6 + 127 = 121 = (01111001)2 and M = (1)2.

The number is stored in the computer as


46 10111100110000000000000000000000
Excess System Example (4)

z Example 3.26 show the value in decimal, when


The bit pattern is stored in Excess_127 format:
(11001010000000000111000100001111)2

Solution
• The first bit is S, the next eight bits, E and the remaining 23 bits, M.
b. The shifter = E − 127 = 148 − 127 = 21.
c. This gives us (1.00000000111000100001111)2 × 221.
d. The binary number is (1000000001110001000011.11)2.
e. The absolute value is 2,104,378.75.
47 f. The number is −2,104,378.75.
Overflow and Underflow

z Overflow and underflow in floating-point


representation of reals (Figure 3.12)

48
Storing Zero

z 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.

49
3.3 Storing Text

50
Overview (1)

z A section of text in any language is a sequence of


symbols used to represent an idea in that language.
z For example, the English language uses
– 26 symbols (A, B, C,…, Z) to represent uppercase letters,
– 26 symbols (a, b, c, …, z) to represent lowercase letters,
– nine symbols (0, 1, 2, …, 9) to represent numeric characters
– symbols (., ?, :, ; , …, !) to represent punctuation.
z Other symbols such as blank, newline, and tab are
used for text alignment and readability.

51
Overview (2)

z Representing each symbol with a bit pattern.


z 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

52 Figure 3.13 Representing symbols using bit patterns


Overview (3)

53
Overview (4)

z Codes (See Appendix A)


– ASCII
– Unicode
– Other Codes

54
3.4 Storing Audio

55
Overview (1)

z Audio
– A representation of sound or music.
– Different to the numbers or text.
z Text is composed of countable entities
(characters); Text is an example of digital data.
– Being not countable.
– An example of analog data.
z Even if we can measure all its values in a period of time, we
cannot store these in the computer’s memory, as we would
need an infinite number of memory locations.
z How to store audio data
56 – Sampling Æ Quantization Æ Encoding
Overview (2)

z Figure 3.15 shows the nature of an analog


signal, such as audio, that varies with time.

57
Sampling

z Sampling means that we select only a finite


number of points on the analog signal,
measure their values, and record them.

58 Figure 3.16 Sampling an audio signal


Quantization

z The value measured for each sample is a real


number.
z However, it is simpler to use an unsigned integer (a
bit pattern) for each sample.
z Quantization refers to a process that rounds the
value of a sample to the closest integer value.
z For example,
– if the real value is 17.2, it can be rounded down to 17: if the
value is 17.7, it can be rounded up to 18.

59
Encoding

z Quantized sample values are encoded as bit patterns.


– Some systems assign positive and negative values to samples,
– Some shift the curve to the positive part and assign only positive
values.
z Number of bits per sample B (bit depth), and the number
of samples per second, S:
– To store S × B bits for each second of audio
z This product is sometimes referred to as bit rate, R. For
example: 40,000 samples per second and 16 bits per
each sample, the bit rate is
60 R = 40,000 × 16 = 640,000 bits per second
Standards for sound encoding

z MP3 (short for MPEG Layer 3).


– Today the dominant standard for storing audio.
– A modification of the MPEG (Motion Picture Experts
Group) compression method used for video.
– Using 44100 samples per second and 16 bits per sample.
z Lossy compression (Chapter 15)
– The result is a signal with a bit rate of 705,600 bits per
second, which is compressed using a compression method
that discards information that cannot be detected by the
human ear.
– As opposed to lossless compression
61
3.5 Storing Images

62
Overview (1)

z Images are stored in computers using two different


techniques:
– Raster graphics and Vector graphics.
z Raster graphics (or bitmap graphics)
– Be used when storing an analog image such as a photograph
– A photograph consists of analog data, similar to audio
information. The difference is that the intensity of data varies in
space instead of in time.
– Data must be sampled. However, sampling in this case is
called scanning. The samples are called pixels (picture
63 elements).
Overview (2)

– When Image scanning, decide how many pixels we


should record for each square or linear inch.
– The scanning rate is called Resolution.
– The number of bits used to represent a pixel, its Color
Depth, depends on how the pixel’s color is handled by
different encoding techniques.
– The perception of color is
z How our eyes respond to a beam of light.
z Our eyes have different types of photoreceptor cells:
some respond to the three primary colors (RGB), while
others merely respond to the intensity of light.
64
True-Color

z One of the encoding techniques


z Using 24 bits to encode a pixel.

65
Indexed Color (Palette color) (1)

z Using only a portion of these colors.

Figure 3.17 Relationship of the indexed color to the True-Color


66
Indexed Color (Palette color) (2)

z For example, a high-quality digital camera


uses almost three million pixels for a 3 × 5
inch photo.
z The following shows the number of bits that
need to be stored using each scheme:

67
Standards for image encoding

z Several de facto standards for image encoding


are in use.
z JPEG (Joint Photographic Experts Group)
uses the True-Color scheme, but compresses
the image to reduce the number of bits
(Chapter 15).
z GIF (Graphic Interchange Format), on the
other hand, uses the indexed color scheme.
68
Vector graphics

z 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.
z The vector graphic image encoding method, however, 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.
z For example, consider a circle of radius r. The main pieces of
information a program needs to draw this circle are:
– The radius r and equation of a circle.
– The location of the center point of the circle.
– The stroke line style and color.
– The fill style and color.
69
3.5 Storing Video

70
Overview

z Video is a representation of images (called frames)


over time.
z A movie consists of a series of frames shown one
after another.
– Video is the representation of information that changes in
space and in time.
– So, if we know how to store an image inside a computer, we
also know how to store video:
– each image or frame is transformed into a set of bit patterns
and stored.
– The combination of the images then represents the video.
z See Chapter 15 for video compression.
71
補充

z r’s and (r-1)’s complement


z IEEE 754

72
r’s Complement

z 若一無號的數字N(N ≠ 0),基底(base)是r,
整數部分的位數為n,則它的r’s complement
定義為(rn − N),且令N = 0時,N的r補數為0。
舉例說明如下:

e.g 6-digit Hex. number N= ABD4F5, What is the 16’s complement of N?

73
(r – 1)’s Complement

z 若一無號的數字N(N ≠ 0),基底(base)是r
,整數部分的位數為n,則它的(r−1)補數為(rn
− 1) − N,且令N = 0時,N的(r−1)補數為0。
舉例說明如下:

74
IEEE 754

Excess 127
single precision

75
IEEE 754

z 小數部分最高有效位由exp部分決定。如果指
數在0 < exponent < 2e − 1之間,那麼小數部
分最高有效位將是1,而且這個數將被稱為正
規形式。
z 如果exp是0,有效數最高有效位將會是0,並
且這個數將被稱為非正規形式。
z 有三個特殊值需要指出:

76
IEEE 754

z 如果 exp 是0 並且 小數部分 是0, 這個數±0 (和符號位


相關)
z 如果 exp = 2e − 1 並且 小數部分 是0, 這個數是 ±無窮
大 (同樣和符號位相關)
z 如果 exp = 2e − 1 並且 小數部分 非0, 這個數表示為
不是一個數(NaN).

77
IEEE 754

78

You might also like