2 Data Storage - NTNUweb.ntnu.edu.tw/~tcchiang/IntroCS/2_Data Storage (I).pdf · One’s complement...
Transcript of 2 Data Storage - NTNUweb.ntnu.edu.tw/~tcchiang/IntroCS/2_Data Storage (I).pdf · One’s complement...
Introduction to Computer Science, Fall, 2010
Data Storage
Instructor: Tsung-Che [email protected]
Department of Computer Science and Information EngineeringNational Taiwan Normal University
Introduction to Computer Science, Fall, 20102
Outline
IntroductionStoring IntegersStoring RealsStoring TextsStoring AudiosStoring ImagesStoring VideosSummary
Introduction to Computer Science, Fall, 20103
Introduction
Data typesData today comes in different forms including
numbers, text, audio, image and video(multimedia).
B. Forouzan and F. Mosharraf, Foundations of Computer Science, 2nd ed., 2008.
Introduction to Computer Science, Fall, 20104
Introduction
Data inside the computerAll data types are
transformed into a uniform representation when theyare stored in a computer and
transformed back to their original form whenretrieved.
This universal representation is called a bitpattern.
B. Forouzan and F. Mosharraf, Foundations of Computer Science, 2nd ed., 2008.
Introduction to Computer Science, Fall, 20105
Introduction
Data inside the computer
B. Forouzan and F. Mosharraf, Foundations of Computer Science, 2nd ed., 2008.
Introduction to Computer Science, Fall, 20106
Storing Numbers
A number is changed to the binary systembefore being stored in the computermemory.
However, there are still two issues thatneed to be handled: how to store the sign of the number how to show the decimal point
Introduction to Computer Science, Fall, 20107
Storing Integers
Integers are whole numbers (numbers without afractional part).
An integer can be thought of as a number in whichthe position of the decimal point is fixed.
For this reason, fixed-point representation is usedto store an integer. In this representation thedecimal point is assumed but not stored.
B. Forouzan and F. Mosharraf, Foundations of Computer Science, 2nd ed., 2008.
Introduction to Computer Science, Fall, 20108
Storing Integers- Unsigned representation
An unsigned integer is an integer that can neverbe negative and can take only 0 or positive values.Its range is between 0 and positive infinity.
An input device stores an unsigned integer usingthe following steps: The integer is changed to binary. If the number of bits is less than n, 0s are added to the
left.
The largest number of the n-bit unsignedrepresentation is .2n –1
Introduction to Computer Science, Fall, 20109
Storing Integers- Unsigned representation
Example 3.1
Store 7 in an 8-bit memory location using unsignedrepresentation.
First change the integer to binary, (111)2.Add five 0s to make a total of eight bits, (00000111)2.Note that the subscript 2 is not stored in the computer.
Solution
B. Forouzan and F. Mosharraf, Foundations of Computer Science, 2nd ed., 2008.
Introduction to Computer Science, Fall, 201010
Storing Integers- Unsigned representation
Example 3.2
Store 258 in a 16-bit memory location.
Example 3.3
What is returned from an output device when it retrieves thebit string 00101011 stored in memory as an unsigned integer?
43.
B. Forouzan and F. Mosharraf, Foundations of Computer Science, 2nd ed., 2008.
Introduction to Computer Science, Fall, 201011
Storing Integers- Unsigned representation
C/C++ Program:#include <cstdio>#include <iostream>using namespace std;
int main(){
unsigned int num = 42;
printf("C: %u %o %x %X\n", num, num, num, num);
cout << "C++: " << showbase<< dec << num << ' '<< oct << num << ' '<< nouppercase << hex << num << ' '<< uppercase << hex << num << ' '<< noshowbase;
system("pause");return 0;
}
C: 42 52 2a 2AC++: 42 052 0x2a 0X2A 請按任意鍵繼續 . . .
Introduction to Computer Science, Fall, 201012
Storing Integers- Unsigned representation
OverflowWhat happens if we try to store an integer that
is larger than 24 −1 = 15 in a memory locationthat can only hold four bits.
B. Forouzan and F. Mosharraf, Foundations of Computer Science, 2nd ed., 2008.
Introduction to Computer Science, Fall, 201013
void display(unsigned int num){
printf("C: %u %o %x %X\n", num, num, num, num);
cout << "C++: " << showbase<< dec << num << ' ‘<< oct << num << ' '<< nouppercase << hex << num << ' '<< uppercase << hex << num << ' '<< noshowbase << endl;
}
int main(){
unsigned int num = std::numeric_limits<unsigned int>::max(),overflow = num+2;
display(num);display(overflow);
system("pause");return 0;
}
Storing Integers- Unsigned representation
#include <cstdio>#include <iostream>#include <limits>using namespace std;
C: 4294967295 37777777777 ffffffff FFFFFFFFC++: 4294967295 037777777777 0xffffffff 0XFFFFFFFFC: 1 1 1 1C++: 1 01 0x1 0X1請按任意鍵繼續 . . .
Introduction to Computer Science, Fall, 201014
Storing Integers- Unsigned representation
Applications CountingAddressingStoring other data types (text, images, audio, and
video)
Introduction to Computer Science, Fall, 201015
Storing Integers- Sign-and-magnitude representation
In this method, the available range for unsignedintegers (0 to 2n −1) is divided into two equal sub-ranges. The first half represents positive integers,the second half, negative integers.
B. Forouzan and F. Mosharraf, Foundations of Computer Science, 2nd ed., 2008.
Note. There are two zeros.
Introduction to Computer Science, Fall, 201016
Storing Integers- Sign-and-magnitude representation
Example 3.4
Store +28 in an 8-bit memory location using sign-and-magnituderepresentation.
The integer is changed to 7-bit binary.The leftmost bit is set to 0. The 8-bit number is stored.
Solution
B. Forouzan and F. Mosharraf, Foundations of Computer Science, 2nd ed., 2008.
Introduction to Computer Science, Fall, 201017
Storing Integers- Sign-and-magnitude representation
Example 3.5
Store -28 in an 8-bit memory location using sign-and-magnituderepresentation.
The integer is changed to 7-bit binary.The leftmost bit is set to 1. The 8-bit number is stored.
B. Forouzan and F. Mosharraf, Foundations of Computer Science, 2nd ed., 2008.
Solution
Introduction to Computer Science, Fall, 201018
Storing Integers- Sign-and-magnitude representation
Example 3.6
Retrieve the integer that is stored as 01001101 insign-and-magnitude representation.
77.
Example 3.7
Retrieve the integer that is stored as 10100001 insign-and-magnitude representation.
-33.
Introduction to Computer Science, Fall, 201019
Storing Integers- Sign-and-magnitude representation
Overflow
B. Forouzan and F. Mosharraf, Foundations of Computer Science, 2nd ed., 2008.
Introduction to Computer Science, Fall, 201020
Storing Integers- Sign-and-magnitude representation
Applications It is not commonly used to store integers, but
it is used to store part of a real number incomputers. (discussed later)
It is also used when we quantize an analog signal,such as audio.
Introduction to Computer Science, Fall, 201021
Storing Integers- Two’s complement representation
Almost all computers use two’s complementrepresentation to store a signed integer.
In this method, the available range for an unsignedinteger of (0 to 2n −1) is divided into two equal sub-ranges. The first sub-range is used to represent nonnegative
integers, the second half to represent negative integers.
B. Forouzan and F. Mosharraf, Foundations of Computer Science, 2nd ed., 2008.
Introduction to Computer Science, Fall, 201022
Storing Integers- Two’s complement representation
One’s complementThis operation simply reverses (flips) each bit.
A 0-bit is changed to a 1-bit, a 1-bit is changedto a 0-bit.
B. Forouzan and F. Mosharraf, Foundations of Computer Science, 2nd ed., 2008.
Introduction to Computer Science, Fall, 201023
Storing Integers- Two’s complement representation
One’s complementWe get the original integer if we apply the one’s
complement operations twice.
B. Forouzan and F. Mosharraf, Foundations of Computer Science, 2nd ed., 2008.
Introduction to Computer Science, Fall, 201024
Storing Integers- Two’s complement representation
Two’s complement (Method 1) First, we copy bits from the right until a 1 is
copied; then, we flip the rest of the bits.
B. Forouzan and F. Mosharraf, Foundations of Computer Science, 2nd ed., 2008.
Introduction to Computer Science, Fall, 201025
Storing Integers- Two’s complement representation
Two’s complement (Method 2) First, take the one’s complement, and thenThen, add 1 to the result.
0 0 1 1 0 1 0 01 1 0 0 1 0 1 1
11 1 0 0 1 1 0 0
+
Original number
One’s complement
Two’s complement
Introduction to Computer Science, Fall, 201026
Storing Integers- Two’s complement representation
Two’s complementWe get the original integer if we apply the
two’s complement operations twice.
B. Forouzan and F. Mosharraf, Foundations of Computer Science, 2nd ed., 2008.
Introduction to Computer Science, Fall, 201027
Storing Integers- Two’s complement representation
To store an integer, the integer is changedto an n-bit binary. If the integer is positive or zero, it is stored as
it is. If it is negative, the computer takes the two’s
complement of the integer and stores it.
Introduction to Computer Science, Fall, 201028
Storing Integers- Two’s complement representation
Example 3.12
Store the integer 28 in an 8-bit memory location using two’scomplement representation.
Example 3.13
Store the integer -28 in an 8-bit memory location using two’scomplement representation.
B. Forouzan and F. Mosharraf, Foundations of Computer Science, 2nd ed., 2008.
Introduction to Computer Science, Fall, 201029
Storing Integers- Two’s complement representation
Example 3.14
Retrieve the integer that is stored as 00001101 in memory intwo’s complement format.
B. Forouzan and F. Mosharraf, Foundations of Computer Science, 2nd ed., 2008.
Introduction to Computer Science, Fall, 201030
Storing Integers- Two’s complement representation
Example 3.15
Retrieve the integer that is stored as 11100110 in memoryusing two’s complement format.
B. Forouzan and F. Mosharraf, Foundations of Computer Science, 2nd ed., 2008.
Introduction to Computer Science, Fall, 201031
Storing Integers- Two’s complement representation
Exercise
Store the integer -45 in an 8-bit memory location using two’scomplement representation.
00101101 11010011(45) (2’s complement)
Exercise
Retrieve the integer that is stored as 10101011 in memoryusing two’s complement format.
10101011 01010101 -85
Introduction to Computer Science, Fall, 201032
void display(int num){
printf("C: %d %X\n", num, num);
cout << "C++: " << showbase<< dec << num << ' ‘<< uppercase << hex << num << ' '<< noshowbase << endl;
}
int main(){
int num = 28;
display(num);display(-num);
system("pause");return 0;
}
Storing Integers- Two’s complement representation
C: 28 1CC++: 28 0X1CC: -28 FFFFFFE4C++: -28 0XFFFFFFE4請按任意鍵繼續 . . .
0000 0000 0000 0000 0000 0000 0001 1100
1111 1111 1111 1111 1111 1111 1110 0100
Introduction to Computer Science, Fall, 201033
Storing Integers- Two’s complement representation
Overflow
B. Forouzan and F. Mosharraf, Foundations of Computer Science, 2nd ed., 2008.
Introduction to Computer Science, Fall, 201034
Storing Integers- Two’s complement representation
void display(int num){
cout << "C++: " << showbase<< dec << num << ' ‘<< uppercase << hex << num << ' '<< noshowbase << endl;
}
int main(){
int max = std::numeric_limits<int>::max(), pOverflow = max+2,min = std::numeric_limits<int>::min(), nOverflow = min-2;
display(max);display(pOverflow);display(min);display(nOverflow);
system("pause");return 0;
}
C++: 2147483647 0X7FFFFFFFC++: -2147483647 0X80000001C++: -2147483648 0X80000000C++: 2147483646 0X7FFFFFFE請按任意鍵繼續 . . .
Introduction to Computer Science, Fall, 201035
Storing Integers- Two’s complement representation
template <class T> void display(T num){
cout << "C++: " << showbase << dec << num << ' ‘<< uppercase << hex << num << ' ' << noshowbase << endl;
}
int main(){
int signed_int = -1;unsigned int unsigned_int = signed_int;display(unsigned_int);
// unsigned constant tailing by 'u'unsigned int unsigned_large = 3*1000*1000*1000u;int signed_small = unsigned_large;display(signed_small);
system("pause");return 0;
}
C++: 4294967295 0XFFFFFFFFC++: -1294967296 0XB2D05E00請按任意鍵繼續 . . .
Introduction to Computer Science, Fall, 201036
Storing Integers- Two’s complement representation
Applications It is the standard representation for storing
integers in computers today.We will see why this is the case when we see
the simplicity of operations using two’scomplement.
Introduction to Computer Science, Fall, 201037
Comparison the Three Systems
B. Forouzan and F. Mosharraf, Foundations of Computer Science, 2nd ed., 2008.
Introduction to Computer Science, Fall, 201038
Storing Reals
Fixed-point representation?
Real numbers with very large integral parts orvery small fractional parts should not be storedin fixed-point representation.
.
5 digits 5 digits
What about 1000000000?What about 0.000000001?
Introduction to Computer Science, Fall, 201039
Storing Reals
Floating-point representationWe can have different numbers of digits to the
left or right of the decimal point. The range can increase tremendously.
A number if made up of three sections: The fixed-point section has only one digit to the left
of the decimal point.
B. Forouzan and F. Mosharraf, Foundations of Computer Science, 2nd ed., 2008.
Introduction to Computer Science, Fall, 201040
Storing Reals
Example 3.18 (Decimal)
The following shows the decimal number
7,452,000,000,000,000,000,000.00
in scientific notation (floating-point representation).
The three sections are the sign (+), the shifter (21) and thefixed-point part (7.425). Note that the shifter is theexponent.
B. Forouzan and F. Mosharraf, Foundations of Computer Science, 2nd ed., 2008.
Introduction to Computer Science, Fall, 201041
Storing Reals
Example 3.19 (Decimal)
Show the number -0.0000000000000232in scientific notation (floating-point representation).
The three sections are the sign (-), the shifter (-14) and thefixed-point part (2.32). Note that the shifter is the exponent.
B. Forouzan and F. Mosharraf, Foundations of Computer Science, 2nd ed., 2008.
Introduction to Computer Science, Fall, 201042
Storing Reals
Example 3.20 (Binary)
Example 3.21 (Binary)
B. Forouzan and F. Mosharraf, Foundations of Computer Science, 2nd ed., 2008.
Introduction to Computer Science, Fall, 201043
Storing Reals
Normalization Leave only one non-zero digit to the left of the
decimal point. In the decimal system, the digit can be 1 to 9,
while in the binary system, it can only be 1.
B. Forouzan and F. Mosharraf, Foundations of Computer Science, 2nd ed., 2008.
Introduction to Computer Science, Fall, 201044
Storing Reals
Sign, exponent, and mantissa
+1000111.0101 becomes
B. Forouzan and F. Mosharraf, Foundations of Computer Science, 2nd ed., 2008.
Note. The point and the bit 1 to the left of the decimal point are not stored.
Introduction to Computer Science, Fall, 201045
Storing Reals
Sign It is stored by one bit.
Exponent It defines the shifting of the decimal point. Note that the power can be negative or positive. The
Excess representation is used to store it.
Mantissa It is the binary integer to the right of the decimal point. The mantissa and the sign is treated like an integer
stored in sign-and-magnitude representation. However, remember that the mantissa is not an integer.
It is a fractional part that is stored like an integer.
Introduction to Computer Science, Fall, 201046
Storing Reals
The Excess SystemThe exponent is a signed number. In the Excess system, both positive and
negative integers are stored as unsignedintegers.
A positive integer (called a bias) is added toeach number to shift them uniformly to thenon-negative side.
The value of this bias is 2m−1 −1, where m is thesize of the memory location to store theexponent.
Introduction to Computer Science, Fall, 201047
Storing Reals
Example 3.22
We can express sixteen integers in a number system with 4-bitallocation. The new system is referred to as Excess-7 (241 –1),or biased representation with biasing value of 7.
B. Forouzan and F. Mosharraf, Foundations of Computer Science, 2nd ed., 2008.
Introduction to Computer Science, Fall, 201048
IEEE Standard
Single-precision & double-precision format
B. Forouzan and F. Mosharraf, Foundations of Computer Science, 2nd ed., 2008.
Introduction to Computer Science, Fall, 201049
IEEE Standard
Example 3.23
Show the Excess_127 (single precision) representation of thedecimal number 6.75.
a. The sign is positive, so S = 0.b. Decimal to binary transformation: 6.75 = (110.11)2.c. Normalization: (110.11)2 = (1.1011)2 × 22.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
B. Forouzan and F. Mosharraf, Foundations of Computer Science, 2nd ed., 2008.
Introduction to Computer Science, Fall, 201050
IEEE Standard
Example 3.24
Show the Excess_127 (single precision) representation of thedecimal number –161.875.
a. The sign is negative, so S = 1.b. Decimal to binary transformation: 161.875= (10100001.111)2.c. Normalization: (10100001.111)2 = (1.0100001111)2 × 27.d. E = 7 + 127 = 134 = (10000110)2 and M = (0100001111)2.e. Representation:
Solution
B. Forouzan and F. Mosharraf, Foundations of Computer Science, 2nd ed., 2008.
Note. 這裡沒有什麼補數不補數了…
Introduction to Computer Science, Fall, 201051
IEEE Standard
Example 3.25
Show the Excess_127 (single precision) representation of thedecimal number –0.0234375.
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
B. Forouzan and F. Mosharraf, Foundations of Computer Science, 2nd ed., 2008.
Introduction to Computer Science, Fall, 201052
IEEE Standard
Example 3.26
The bit pattern (11001010000000000111000100001111)2 isstored in Excess_127 format. Show the value in decimal.
a. The sign is negative.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.f. The number is −2,104,378.75.
Solution
Introduction to Computer Science, Fall, 201053
IEEE Standard
Storing zeroA real number with an integral part and the
fractional part set to zero, that is, 0.0, cannotbe stored using the steps discussed above.
To handle this special case, it is agreed that inthis case the sign, exponent and the mantissaare set to 0s.
Truncation errorsWhen a real number is stored using floating-
point representation, the value stored may notbe exactly as we expect it to be.
Introduction to Computer Science, Fall, 201054
IEEE Standard
Online converter http://www.h-schmidt.net/FloatApplet/IEEE754.html http://babbage.cs.qc.edu/IEEE-754/