CSI104 Slot05
CSI104 Slot05
DATA STORAGE
1. Data types
2. Storing Numbers
3
Objectives
4
CONTENT
1. Data types
2. Storing Numbers
5
INTRODUCTION
Data today comes in different forms including numbers, text, audio, image,
and video.
Data
• All data types are transformed into a uniform representation when stored in a
computer and transformed back to their original form when retrieved.
• This universal representation is called a bit pattern or a sequence of 0s and 1s.
1 0 0 0 1 0 1 0 1 1 1 1 1 1 0 1
• Bits: The symbol 0 or 1 is called a bit (binary digit), the smallest unit of data that can
be stored in a computer.
• Bits patterns: a sequence, or a string of bits. A bit patterns with 8 bits is called a byte.
7
STORAGE OF DIFFERENT DATA TYPES
9
INTRODUCTION
• A number is changed to the binary system before being stored in the computer
memory. Two issues exist:
1. How to store the sign of the number
2. How to show the decimal point
• There are several ways to handle the sign issue (discussed later).
• For the decimal point, computers use two different representations: fixed-point
and floating point.
To store
To store an integer
a real
10
2.1. STORING INTEGERS
0 1 1 0 1 1 1 0 0 0 1 0 0 1 1 0 ●
Memory location
Decimal point
(assumed position)
Figure 3.4. Fixed-point representation of integers 11
UNSIGNED Representation
Change 7 to binary à 1 1 1
14
SIGN-AND-MAGNITUDE Representation
• The available range for unsigned integers [0 . . 2! − 1] is divided into two equal sub-
ranges. The 1st half: positive integers; the 2nd half: negative integers.
• The range of numbers that can be stored in an n-bit location is
− 2!"# − 1 to + 2!"# − 1
• Note: there are two different representations for zero.
0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111
0 1 2 3 4 5 6 7 -0 -1 -2 -3 -4 -5 -6 -7
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.
15
Overflow in SIGN-AND-MAGNITUDE representation
We may have both positive and negative overflow with signed integers.
17
TWO’S COMPLEMENT Representation
Almost all computers use two’s complement representation to store a signed integer
in an 𝑛-bit memory location.
• For example, if 𝑛 = 4, the range is 0000 to 1111. This range is divided into two halves:
0000 to 0111 and 1000 to 1111.
Although the sign of the integer affects every bit in the binary integer stored, the first
(leftmost) bit determines the sign.
18
TWO’S COMPLEMENT Representation
Original integer 0 0 1 1 0 1 0 0
↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
Two’s complementing once 1 1 0 0 1 1 0 0
19
TWO’S COMPLEMENTING (cont.)
Example: Store 34 and −34 in 8-bit memory locations using 2’s complement representation
20
TWO’S COMPLEMENTING (cont.)
Example: Retrieve the integer that is stored as 10001101 in memory in two’s complement format
1. Since the left most bit is 1, 2’s complement operation is applied
1 0 0 0 1 1 0 1
à 0 1 1 1 0 0 1 1
2. Change to decimal: (01110101)! = (117)"#
3. Sign is added: −117
21
Overflow in two’s complement representation
Figure 3.7. Overflow in two’s complement representation using a 4-bit memory location 22
2.2. Storing Reals
Example:
In the decimal system, assume that we use a fixed-point representation with two digits at the
right of the decimal point and fourteen digits at the left of the decimal point, for a total of
sixteen digits. The precision of a real number in this system is lost if we try to represent a
decimal number such as 1.00234. The system stores the number as 1.00.
Real numbers with very large integral parts or very small fractional parts should
not be stored in fixed-point representation.
23
Storing Reals: Floating-Point Representation
• Normalization: to make the fixed part of the representation uniform, both the
scientific method (for the decimal system) & the floating-point method (for the
binary system) use only one non-zero digit on the left of the decimal point.
• Sign: can be stored using 1 bit
• Exponent: The exponent (power of 2) defines the shifting of the decimal point.
• Mantissa: is a fractional part that, together with the sign, is treated like an integer
stored in sign-and-magnitude representation.
Two most common standards for storing floating-point numbers: Single precision
(Excess_27), and Double precision (Excess_1023).
A real number is stored using these steps: Show the Excess_127 (single precision)
representation of the decimal number 5.75.
1. Store the sign in S (0 or 1). 1. S=0
3. Normalize. 3. 1.0111 x 22
1. Data types
2. Storing Numbers
29
3.1 Storing Text
30
Storing Text: Codes
• Unicode: Using 32 bits to represent up to 2$! symbols (including graphical and special
symbols). Suitable for the communication in multiple languages. ASCII and extended ASCII
are parts of Unicode.
• Other Codes
Number of symbols Bit pattern length Number of symbols Bit pattern length
2 1 128 7
4 2 257 8
8 3 65,536 16
16 4 4,294,967,296 32
Table 3.8. Number of symbols and bit pattern length
31
3.2 Storing Audio
Audio:
• is a representation of sound or music
• is not countable
• varies with time
• is an example of analog data. Even if we
are able to measure all its values in a
period of time, we cannot store these in Figure 3.9. An audio signal
the computer’s memory, as we would
need an infinite number of memory
locations.
32
Storing Audio: Sampling
If we cannot record all the values of an audio signal over an interval, we can record
some of them.
Sampling means that we select only a finite number of points on the analog signal,
measure their values, and record them.
A sampling rate of 𝟒𝟎, 𝟎𝟎𝟎 samples per second is generally good enough.
• The value measured for each sample is a real number. This means that we can store
40,000 real values for each one-second sample. However, it is simpler to use an unsigned
integer (a bit pattern) for each sample.
• Quantization refers to a process that rounds the value of a sample to the closest integer
value.
o For example, if the real value is 17.2 à 17; if the value is 17.7 à 18.
34
Storing Audio: Encoding
35
Storing Audio: Standards for Sound Encoding
• Today the dominant standard for storing audio is MP3 (short for MPEG Layer 3).
• This standard is a modification of the MPEG (Motion Picture Experts Group)
compression method used for video. It uses 44,100 samples per second and 16
bits per sample.
• 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. This is called lossy compression, as opposed to
lossless compression.
36
3.3 Storing Images
Images are stored using two different techniques: raster graphics and vector graphics
Raster graphics
• Raster graphics (or bitmap graphics) is used to store an analog image (e.g., a photograph).
o A photograph consists of analog data, similar to audio information. The difference is that the
intensity (color) of data varies in space instead of in time.
à Data must be sampled. Sampling in this case is normally called scanning. The samples are
called pixels (picture elements).
• Resolution: in image scanning we need to decide how many pixels we should record for each
square or linear inch. The scanning rate in image processing is called resolution.
37
Color
Color Depth : The number of bits used to represent a pixel, depends on how the pixel’s color is
handled by different encoding techniques. The perception of color is how our eyes respond to a
beam of light.
• True-Color: a technique used to encode a
pixel is called True- Color, which uses 24
bits to encode a pixel
38
Standards for Image Encoding
39
Vector Graphics
• Raster graphics has two disadvantages: the file size is big, and rescaling is troublesome.
• The vector graphic image encoding method 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. The main pieces of information a program needs to
draw this circle are:
1. The radius r and equation of a circle
2. The location of the center point of the circle
3. The stroke line style and color
4. The fill style and color
41
3.2 Operations on
Data
Content
• Logic
• Shift
• Arithmetic operations
43
Objectives
44
CONTENT
• NOT
• Logic
• AND
• OR
• Shift
• XOR
• Arithmetic operations
45
LOGIC operations: Introduction
46
LOGIC operations: NOT operator
The NOT operator is a unary operator: it takes only one input. The output bit is
the complement of the input.
NOT 1 0 0 1 1 0 0 0 Input
0 1 1 0 0 1 1 1 Output
47
LOGIC operations: AND operator
The AND operator is a binary operator: it takes two inputs. The output bit is 1 if
both inputs are 1s and the output is 0 in the other three cases.
Example: Use the AND operator on the bit patterns 10011000 and 00101010.
Solution: The solution is shown below:
AND 1 0 0 1 1 0 0 0 Input 1
0 0 1 0 1 0 1 0 Input 2
0 0 0 0 1 0 0 0 Output
48
LOGIC operations: OR operator
The OR operator is a binary operator: it takes two inputs. The output bit is 0 if
both inputs are 0s and the output is 1 in other three cases.
Example: Use the OR operator on the bit patterns 10011001 and 00101110.
Solution: The solution is shown below:
OR 1 0 0 1 1 0 0 1 Input 1
0 0 1 0 1 1 1 0 Input 2
1 0 1 1 1 1 1 1 Output
For 𝑥 = 0 or 1 𝑥 OR 1 → 1 and 1 OR 𝑥 → 1
49
LOGIC operations: XOR operator
The XOR operator is a binary operator like the OR operator, with only one
difference: the output is 0 if both inputs are 1s.
Example: Use the XOR operator on the bit patterns 10011001 and 00101110
Solution: The solution is shown below:
XOR 1 0 0 1 1 0 0 1 Input 1
0 0 1 0 1 1 1 0 Input 2
1 0 1 1 0 1 1 1 Output
50
CONTENT
• Logic
• Shift • Logical
o Simple
• Arithmetic operations o Circular
• Arithmetic
51
SHIFT operations: Introduction
• SHIFT operations move the bits in a pattern, changing the positions of the bits.
They can move bits to the left or to the right.
• Two categories: logical shift operations & arithmetic shift operations.
• Logical shift: A logical shift operation is applied to a pattern that does not
represent a signed number.
• Because these shift operations may change the sign of the number that is defined
by the leftmost bit in the pattern. Two types of logical shift operations:
• Simple shift
• Circular shift (Rotate)
52
SHIFT operations: Logical Right/Left Simple Shift
Example: Use a logical left shift operation on the bit pattern 10011000.
Solution
The leftmost bit is lost and a 0 is inserted as the rightmost bit.
53
SHIFT operations: Logical Right/Left Circular Shift
Example: Use a circular left shift operation on the bit pattern 10011000.
Solution
The solution is shown below. The leftmost bit is circulated and becomes the rightmost bit.
54
4. Arithmetic Shift Operations
Arithmetic shift operations assume that the bit pattern is a signed integer in two’s
complement format. Arithmetic right shift: divide an integer by two; arithmetic left shift:
multiply an integer by two.
Example: Use an arithmetic right shift operation on the bit pattern 10011001. The pattern is an
integer in two’s complement format.
Solution: The solution is shown below
-103
-52 55
CONTENT
• Logic
• Shift
• Arithmetic operations
56
Introduction
Example: Two integers A and B are stored in two’s complement format. Show how B is
added to A.
𝐴 = (00010001)$ 𝐵 = (00010110)$
1 Carry
0 0 0 1 0 0 0 1 A
+ 0 0 0 1 0 1 1 0 B
0 0 1 0 0 1 1 1 R
59
Addition and subtraction of integers in sign-and-magnitude
Example: Two integers A and B are stored in sign-and-magnitude format. Show how B is
subtracted from A.
A = (𝟏 1010001), B = (𝟏 0010110),
Solution
The operation is subtracting: 𝐵- = 𝐵- = 0. 𝑆 = (𝐴- 𝑋𝑂𝑅 𝐵- ) = (1 𝑋𝑂𝑅 0) = 1.
𝑅. = 𝐴. + (𝐵. + 1). There’s an overflow à 𝑅. is final.
• All arithmetic operations such as addition, subtraction, multiplication and division can be applied
to reals stored in floating-point format.
• Multiplication of two reals involves multiplication of two integers in sign-and-magnitude
representation.
• Division of two reals involves division of two integers in sign-and-magnitude representations
62
Addition and subtraction of reals in floating-point format
Example
Show how the computer finds the result of (+5.75) + (+161.875) = (1167.625).
Solution
These two numbers can be stored in floating-point format, as shown below:
De-normalize: adding the hidden 1s to the mantissa & incrementing the exponent
64
Addition and subtraction of reals in floating-point format (cont.)
Align the mantissas: increment the first exponent & shift its mantissa to the right
65
Addition and subtraction of reals in floating-point format (cont.)
66