CH02 英文PPT
CH02 英文PPT
2
Chapter
An Introduction to Computer Science 02
3
Chapter
An Introduction to Computer Science 02
Please refer to
https://en.wikipedia.org/wiki/Bit and
https://en.wikipedia.org/wiki/Byte 。
6
Chapter
An Introduction to Computer Science 02
7
Chapter
An Introduction to Computer Science 02
9
Chapter
An Introduction to Computer Science 02
10
Chapter
An Introduction to Computer Science 02
11
Chapter
An Introduction to Computer Science 02
資 訊 專 欄
Regarding units of data capacity, commonly used ones
include KB, MB, GB, and TB.
「B」Apologies for the oversight. It represents bytes, not
bits.
「K」It represents 210, which is 1,024, approximately around
one thousand.
12
Chapter
An Introduction to Computer Science 02
資 訊 專 欄
「M」220 = 210 × 210 = 1,048,576, approximately
around one million.
For estimation purposes of 2x to the power of x, we
often use 210 as a simplified shortcut, because it is very
close to 103 (which is 1000).
13
Chapter
An Introduction to Computer Science 02
資 訊 專 欄
14
Chapter
An Introduction to Computer Science 02
15
Chapter
An Introduction to Computer Science 02
Data type
Numbers
Text
Audio, Music
Graphics, Images
Videos and animations
16
Chapter
An Introduction to Computer Science 02
17
Chapter
An Introduction to Computer Science 02
18
Chapter
An Introduction to Computer Science 02
19
Chapter
An Introduction to Computer Science 02
20
Chapter
An Introduction to Computer Science 02
21
Chapter
An Introduction to Computer Science 02
523
5 2 3
In the hundreds place, it In the tens place, it represents In the ones place, it represents
represents five hundreds. two tens. three ones.
Here, B0 = 1
23
Chapter
An Introduction to Computer Science 02
24
Chapter
An Introduction to Computer Science 02
25
Chapter
An Introduction to Computer Science 02
2-3 Conversion of various numeral
systems
Hexadecimal Numeral System Symbol and Its
Corresponding Decimal and Binary Equivalents
26
Chapter
An Introduction to Computer Science 02
資 訊 專 欄
The Taipei 101 building was completed in 2004 and
was known as the world's tallest building at that time.
The author of this book, Mr. Zhao, has been living on
the 1011th floor since the last century. What? 1011?
Isn't that an exceptionally high floor? Ah-ha!
27
Chapter
An Introduction to Computer Science 02
資 訊 專 欄
Actually, it's in binary: 1011 in binary equals 1×23 +
0×22 + 1×21 + 1 = 11 in decimal, so it's the 11th floor!
To avoid confusion, if a number is not in decimal
representation, we usually denote its base to the right
of the number. For example, 10112 indicates binary
representation.
28
Chapter
An Introduction to Computer Science 02
Interconversion of Decimal and
Binary Numbers
Binary Number dndn-1...d2d1.r1r2...rm-1rm The represented
number is:
dn×2n-1 + dn-1×2n-2 + ... + d2×21 + d1 + r1×2-1 + r2×2-2 + ...
+ rm-1×2-(m-1) + rm×2-m
Example1
10110101.11012 The corresponding decimal number is:181.8125
30
Chapter
An Introduction to Computer Science 02
Example2
The binary representation of the decimal number 181 is:101101012
31
Chapter
An Introduction to Computer Science 02
Example3
The binary representation of the decimal number 0.8125 is 0.11012
0.8125×2=1.625
→r1 is 1
remaining decimal 0.625×2=
1.25
→r2 is 1
…Continuing the process.
32
Chapter
An Introduction to Computer Science 02
Example4
The binary representation of the
decimal number 0.1 is an infinitely
repeating sequence.0.000110011...2
33
Chapter
An Introduction to Computer Science 02
Conversion between binary and
hexadecimal numbers
Because 16 is a power of 2, binary and hexadecimal
numbers can be said to come from the same origin.
34
Chapter
An Introduction to Computer Science 02
Example5
The hexadecimal representation of 110110101.11012 is 1B5.D816
35
Chapter
An Introduction to Computer Science 02
Example6
The binary representation of 1B5.D81616 is 110110101.110112
36
Chapter
An Introduction to Computer Science 02
資 訊 專 欄
The phrase "28 years old" is often used to describe the
youthful years full of potential, referring to friends
who are around 16 years old, as "2 times 8" equals
about 16 years old.
37
Chapter
An Introduction to Computer Science 02
資 訊 專 欄
However, the author of this book, Zhao Lao, was already an
experienced person when he wrote the first edition, thus it's
quite surprising that he claims to have just passed his "28
years old". What's going on here? It turns out it's in
hexadecimal, where "28" is equal to 2 times 161 + 8 = 40.
It's said that "28 years old should still be there, it's just a
different base".
38
Chapter
An Introduction to Computer Science 02
39
Chapter
An Introduction to Computer Science 02
Unsigned integers.
A correspondence table between binary strings and decimal
numbers.
40
Chapter
An Introduction to Computer Science 02
Representation with sign and
magnitude notation.
The most straightforward method to represent both positive and
negative numbers is by using the 'signed magnitude notation'.
The leftmost bit of the binary string serves as the sign bit (0 for
positive numbers; 1 for negative numbers), while the remaining
n-1 bits are used to represent the magnitude of the number.
The range of integers starting with the bit 0 is 0~2n-1-1
The range of integers starting with the bit 1 is 0~-(2n-1-1)
41
Chapter
An Introduction to Computer Science 02
43
Chapter
An Introduction to Computer Science 02
44
Chapter
An Introduction to Computer Science 02
45
Chapter
An Introduction to Computer Science 02
46
Chapter
An Introduction to Computer Science 02
47
Chapter
An Introduction to Computer Science 02
48
Chapter
An Introduction to Computer Science 02
49
Chapter
An Introduction to Computer Science 02
steps 2:If the binary value exceeds n-1 bits, it will result
in an overflow and cannot be converted; otherwise,
prepend zeros to its left until there are a total of n bits.
steps 3:If the number to be converted is positive or zero,
the value obtained in step 2 is the desired result; if it is
negative, each bit is converted using the complement
(original 0 becomes 1; original 1 becomes 0).
50
Chapter
An Introduction to Computer Science 02
Example7
What is the one's complement representation of 41?
First step
Convert 41 to binary value: 101001.
Second step
Prepend zeros to the left of the binary value 00101001 until
there are a total of 8 bits, because the number to be
represented is positive. Therefore, 00101001 is the desired
result.
51
Chapter
An Introduction to Computer Science 02
Example8
What is the one's complement representation of -41?
First step
Convert 41 to binary value: 101001.
Second step
Prepend zeros to the left of the binary value 00101001 until
there are a total of 8 bits. Since it represents a negative
number, invert each bit (0 becomes 1, 1 becomes 0),
resulting in 11010110.
52
Chapter
An Introduction to Computer Science 02
53
Chapter
An Introduction to Computer Science 02
Example9
What is the value represented by the one's complement '11010110'?
First step
Since the leftmost bit is 1, we invert each bit (0 becomes
1, 1 becomes 0), resulting in 00101001.
Second step
We then convert the binary number 00101001 to its
decimal equivalent, which is 41. Adding a negative sign
to it, we get -41.
54
Chapter
An Introduction to Computer Science 02
55
Chapter
An Introduction to Computer Science 02
56
Chapter
An Introduction to Computer Science 02
57
Chapter
An Introduction to Computer Science 02
Complement method
58
Chapter
An Introduction to Computer Science 02
Example10
What is the two's complement representation of 40?
First step
First, convert 40 into its binary representation 101000.
Second step
Pad the binary number on the left with zeros to make it
8 bits long, so 00101000 becomes the representation.
Since we are representing a positive number, 00101000
is the result.
61
Chapter
An Introduction to Computer Science 02
Example11
What is the two's complement representation of -40?
First step
First, convert 40 into its binary representation: 101000.
Second step
Pad the binary number on the left with zeros to make it 8 bits
long. Since we are representing a negative number, keep the
rightmost three zeros and the first one unchanged, while
inverting the rest of the bits (original 0 becomes 1; original 1
becomes 0). This gives us 11011000.
62
Chapter
An Introduction to Computer Science 02
Example12
What is the value represented by the two's complement number '11011000'?
First step
The leftmost bit is 1, so first preserve the rightmost three
zeros and the first one. Then, invert the other bits (original 0
becomes 1; original 1 becomes 0), resulting in 00101000.
Second step
Convert the binary number 00101000 to its decimal
representation, which is 40. Then, add a negative sign,
resulting in -40.
63
Chapter
An Introduction to Computer Science 02
Two's complement
representation
In two's complement
representation, there is only one
representation for zero, which is
00000000 for an eight-bit number.
Correspondence between the bit
string and the numerical value in
eight-bit two's complement
representation
64
Chapter
An Introduction to Computer Science 02
65
Chapter
An Introduction to Computer Science 02
66
Chapter
An Introduction to Computer Science 02
Example13
Adding two positive numbers using the two's complement representation.
67
Chapter
An Introduction to Computer Science 02
Example14
Adding one positive and one negative number using the two's complement representation, with the result being positive.
68
Chapter
An Introduction to Computer Science 02
Example15
Adding one positive and one negative number using the two's complement representation, resulting in a negative value.
69
Chapter
An Introduction to Computer Science 02
Example16
Adding two negative numbers using the two's complement representation.
70
Chapter
An Introduction to Computer Science 02
Example17
When adding two positive numbers using the two's complement representation, if the result
exceeds the range of positive numbers that can be stored, it is called overflow.
The result of adding the sign bits is 1, indicating that adding two
positive numbers results in a negative number. This is because in an 8-
bit two's complement representation, the maximum positive number is
2n-1-1 (=127), while in this case, the result is 129, which exceeds the
range of positive numbers that can be stored.
71
Chapter
An Introduction to Computer Science 02
Example18
When adding two negative numbers using the two's complement representation, if the result is smaller than the range
of negative numbers that can be stored, it is called overflow.
The result of adding the sign bits is 0, indicating that adding two
negative numbers results in a positive number. This is because in an 8-
bit two's complement representation, the smallest negative number is
-2n-1 (=-128), while in this case, the result is -129, which is smaller
than the range of negative numbers that can be stored.
72
Chapter
An Introduction to Computer Science 02
The addition of two's complement
numbers.
Two's complement addition involving negative numbers can be more
complex.
The binary string for 40 is 00101000, and for -40 in two's
complement representation, it is 11011000. Treating the sign bit as
part of the numerical value, converting the binary strings to decimal,
we get 216, which is exactly 256 - 40, equivalent to 28 - 40.
The binary string for 24 is 00011000, and for -24 in two's
complement representation, it is 11101000. Considering the sign bit
as part of the numerical value, converting the binary strings to
decimal, we get 232, which is exactly 256 - 24, equivalent to 28 - 24.
73
Chapter
An Introduction to Computer Science 02
74
Chapter
An Introduction to Computer Science 02
75
Chapter
An Introduction to Computer Science 02
76
Chapter
An Introduction to Computer Science 02
77
Chapter
An Introduction to Computer Science 02
78
Chapter
An Introduction to Computer Science 02
10110.100011 1.0110100011×24
79
Chapter
An Introduction to Computer Science 02
80
Chapter
An Introduction to Computer Science 02
81
Chapter
An Introduction to Computer Science 02
82
Chapter
An Introduction to Computer Science 02
83
Chapter
An Introduction to Computer Science 02
84
Chapter
An Introduction to Computer Science 02
Example19
Given a real number 10110.100011, what is its binary representation?
First step
The number is converted to 1.0110100011 times 24 . Since it's a
positive number, the sign bit is 0. The mantissa part is 0110100011,
and the exponent part is 4. By using the Excess 127 representation,
we add 127 to the exponent, resulting in 131.
Second step
131 in binary is 10000011. Therefore, if stored according to the IEEE
754 standard, 10110.100011 would be represented as
01000001101101000110000000000000.
85
Chapter
An Introduction to Computer Science 02
Example20
Given a real number -0.0010011, what is its binary representation?
First step
The number is converted to -1.0011 times 2-3. Since it's a
negative number, the sign bit is 1. The mantissa part is 0011,
and the exponent part is -3. By using the Excess 127
representation, we add 127 to the exponent, resulting in 124.
Second step
124 in binary is 01111100. Therefore, if stored according to
the IEEE 754 standard, -0.0010011 would be represented as
10111110000110000000000000000000.
86
Chapter
An Introduction to Computer Science 02
Example21
01000010100101000110000000000000 所儲存的數值為多少?
First step
The sign bit is 0, indicating it's a positive number. The
exponent part is 10000101, which in decimal is 133.
Subtracting 127 from 133, we get 6.
Second step
01000010100101000110000000000000 stored value is
1.0010100011×26:1001010.0011。
87
Chapter
An Introduction to Computer Science 02
Example22
What is the value stored by 10000010100101000110000000000000?
First step
The sign bit is 1, indicating it's a negative number. The
exponent part is 00000101, which in decimal is 5.
Subtracting 127 from 5, we get -122.
Second step
10000010100101000110000000000000 is -
1.0010100011×2-122.
88
Chapter
An Introduction to Computer Science 02
The range of numbers that can be represented
by single-precision floating-point numbers.
The smallest positive number
00000000100000000000000000000000,
Its value is+2-126.
The largest positive number
01111111011111111111111111111111,
Its value is (2 - 2-23) × 2127.
89
Chapter
An Introduction to Computer Science 02
90
Chapter
An Introduction to Computer Science 02
91
Chapter
An Introduction to Computer Science 02
92
Chapter
An Introduction to Computer Science 02
93
Chapter
An Introduction to Computer Science 02
94
Chapter
An Introduction to Computer Science 02
Unicode
The Unicode character encoding standard, established by the
Unicode Consortium between 1988 and 1991, has become an
ISO-certified standard (ISO 10646).
Unicode has developed the following multiple encoding schemes:
UTF-8 is the most commonly used encoding on the World
Wide Web.
UTF-16 is adopted by JAVA and Windows.
UTF-32 is used by some UNIX systems.
95
Chapter
An Introduction to Computer Science 02
Unicode
96
Chapter
An Introduction to Computer Science 02
97
Chapter
An Introduction to Computer Science 02
The website http://www.unicode.org/charts/ provides tables for various
categories of characters for reference.
98
Chapter
An Introduction to Computer Science 02
EBCDIC
In addition to ASCII and Unicode, IBM's EBCDIC is also a
commonly used encoding on certain models of
computers.
The International Organization for Standardization (ISO)
uses a four-byte (32-bit) encoding scheme, allowing for
232 combinations, which can represent up to
4,294,967,296 characters.
99
Chapter
An Introduction to Computer Science 02
Big5 / GB
For Traditional Chinese characters, Big5 encoding,
which encompasses approximately 16,000
characters, is widely popular and prevalent in
Taiwan and Hong Kong.
For Simplified Chinese characters, GB encoding, which
includes around 8,000 characters, is widely popular and
prevalent in mainland China.
100
Chapter
An Introduction to Computer Science 02
101
Chapter
An Introduction to Computer Science 02
資 訊 專 欄
Unicode
In practical applications, Unicode does not always
store characters in 16 bits.
102
Chapter
An Introduction to Computer Science 02
資 訊 專 欄
Take UTF-8, for example: traditional ASCII characters
are still stored in one byte (with the first bit being 0,
and the remaining 7 bits representing the original
ASCII encoding).
For non-ASCII characters, different lengths of
encoding are used depending on the category.
103
Chapter
An Introduction to Computer Science 02
資 訊 專 欄
• In UTF-16, the encoding for 「A」 is 「 0041 」
• In UTF-8, it's 「 41 」
• For 「趙」, its UTF-16 encoding is 「 8D99」, and in
UTF-8, it's 「 E8B699」.
104
Figure 4.1 Logic operations at the bit level
105
4.1.2 Logic operations at pattern level
The same four operators (NOT, AND, OR, and XOR) can be
applied to an n-bit pattern. The effect is the same as
applying each operator to each individual bit for NOT and to
each corresponding pair of bits for other three operators.
Figure 4.2 shows these four operators with input and output
patterns.
106
Example 4.3
Solution
The solution is shown below. Note that the NOT operator changes every 0 to 1 and
every 1 to 0.
107
Example 4.4
Use the AND operator on the bit patterns 10011000 and 00101010.
Solution
The solution is shown below. Note that only one bit in the output is 1, where both
corresponding inputs are 1s.
108
Example 4.5
Solution
The solution is shown below. Note that only one bit in the output is 0, where both
corresponding inputs are 0s.
109
Example 4.6
Use the XOR operator on the bit patterns 10011001 and 00101110.
Solution
The solution is shown below. Compare the output in this example with the one in
Example 4.5. The only difference is that when the two inputs are 1s, the result is 0
(the effect of exclusion).
110
Applications
Complementing (NOT)
Unsetting (AND)
Setting (OR)
Flipping (XOR)
111
Example 4.7
Use a mask to unset (clear) the five leftmost bits of a pattern. Test the mask with the
pattern 10100110.
Solution
The mask is 00000111. The result of applying the mask is:
112
Example 4.8
Use a mask to set the five leftmost bits of a pattern. Test the mask with the pattern
10100110.
Solution
The mask is 11111000. The result of applying the mask is:
113
Example 4.9
Use a mask to flip the five leftmost bits of a pattern. Test the mask with the pattern
10100110.
Solution
The mask is 11111000. The result of applying the mask is:
114
Example 3.1
Store 7 in an 8-bit memory location using unsigned
representation.
Solution
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. Note that the subscript 2 is used to emphasize
that the integer is binary, but the subscript is not stored in the
computer.
115
Example 3.2
Store 258 in a 16-bit memory location.
Solution
First change the integer to binary (100000010)2. Add seven 0s to
make a total of sixteen bits, (0000000100000010)2. The integer
is stored in the memory location.
116
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.
117
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.
Figure 3.5 Overflow in unsigned integers
118
Sign-and-magnitude representation
In this method, the available 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.
Figure 3.6 Sign-and-magnitude representation
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.
119
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.
120
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.
121
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.
122
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.
123