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

CH02 英文PPT

Uploaded by

iamaduckww
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

CH02 英文PPT

Uploaded by

iamaduckww
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 123

CHAPTER 02 Digital data representation

2-1 data type

2-2 binary representation

2-3 Conversion between

different numeral systems

2-4 integer representation

2-5 floating-point representation

1 2-6 ASCII and Unicode


Chapter
An Introduction to Computer Science 02

What exactly is digital?


In electronics, digital refers to the representation of quantities through
discrete changes.
What does discrete change mean?
 Real numbers represent continuous changes because between any
two numbers, you can always find a third number that lies between
them, and ultimately there are no gaps.
 Integers represent discrete changes. For example, between the
integer 1 and the integer 2, we cannot find any other integer that lies
between them.

2
Chapter
An Introduction to Computer Science 02

What exactly is digital?

For representing discrete changes in quantity,


combinations of binary digits, also known as bits,
can be used for counting.
A bit is the basic unit of digital information and the
smallest unit used for storing or transmitting data
in computers. It is commonly represented by 0 or 1.

3
Chapter
An Introduction to Computer Science 02

What exactly is digital?

Computers use bits to represent data primarily


because electronic components have two stable
states, each represented by a single 0 or 1, known
as a bit.
 「Open」(commonly used to represent "1")
 「Closed」(commonly used to represent "0")
4
Chapter
An Introduction to Computer Science 02

What exactly is digital?


In the early days of computing, the access unit was comprised
of 8 bits, thus 8 bits became known as a byte.
Two bits can have a total of 2 to the power of 2 combinations,
giving 4 possible combinations (00, 01, 10, 11).
With each additional bit, the number of combinations
doubles.
With n bits, there can be 2^n different combinations, which
can represent 2^n different objects.
5
Chapter
An Introduction to Computer Science 02

What exactly is digital?

Please refer to
https://en.wikipedia.org/wiki/Bit and
https://en.wikipedia.org/wiki/Byte 。

6
Chapter
An Introduction to Computer Science 02

The various combinations of one to five bits

7
Chapter
An Introduction to Computer Science 02

What exactly is digital?


Eight bits can have 2 to the power of 8, totaling 256
combinations, which is sufficient to represent every
English letter (a total of 52 including both
uppercase and lowercase), every digit (10 in total
from 0 to 9), and punctuation marks. ASCII is the
established standard for these types of
combinations.
8
Chapter
An Introduction to Computer Science 02

What exactly is digital?

With 16 bits, there can be 2 to the power of 16,


totaling 65,536 combinations, far exceeding the
number of commonly used Chinese characters.
Therefore, 16 bits can represent Chinese characters.

9
Chapter
An Introduction to Computer Science 02

What exactly is digital?

To avoid conflicts in the representation of


characters from various languages, Unicode
standardizes the representation of characters from
different countries using combinations of different
numbers of bits, depending on the implementation.

10
Chapter
An Introduction to Computer Science 02

Because the access mechanism of computers is


based on bytes, the number of bits required to
represent data is typically 8, 16, or 32.

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

2-1 Data types

Computers deal with various data types such as


numbers, text, speech, music, graphics, images,
videos, and animations. These are encoded into bit
strings stored in the computer, and when displayed
or printed, they are decoded back into their original
data formats.

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

2-1 Data types


Digital image conversion
 Taking black and white photos as an example, a small
portion of the photo records the grayscale of each
square (ranging from 0 to 255), and each square can
be represented using eight bits (eight 0s and 1s can
have 256 combinations). The same principle can be
applied to digitize color images.

17
Chapter
An Introduction to Computer Science 02

Digital audio conversion


 Sampling on CD discs occurs approximately
44,100 times per second. Each sampled sound
wave can be converted into a corresponding bit.

18
Chapter
An Introduction to Computer Science 02

2-1 Data types


Digital information facilitates editing, processing,
storage, transmission, and playback, enabling more
efficient and precise expression of ideas.
Computers can be used to edit and integrate various
digital information, accurately arranging the sequence,
timing, and playback devices of various complex media
appearances.

19
Chapter
An Introduction to Computer Science 02

2-1 Data types


Computers' powerful processing and search capabilities
can be leveraged to provide multimedia interactive
methods, enhancing the realism of virtual reality.
The ubiquitous pull of the Internet enables the
instantaneous transmission of digitized information to
every corner of the world.

20
Chapter
An Introduction to Computer Science 02

2-2 Binary representation


The number system used by the ancient Babylonians
was sexagesimal, based on sixty. It advanced one unit
for every sixty, and while remnants of it remain with
sixty seconds in a minute and sixty minutes in an hour,
it's largely fallen out of use.
The modern metric system uses base ten, known as the
decimal system, where every ten units advance by one.

21
Chapter
An Introduction to Computer Science 02

2-2 Binary representation


The value represented by a digit varies depending on its
position.

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.

523 = 5×102 + 2×101 + 3。


22
Chapter
An Introduction to Computer Science 02

2-2 Binary representation


If B is the base, then the number represented by dndn-
1...d2d1.r1r2...rm-1rm :

dn×Bn-1 + dn-1×Bn-2 + ... + d2×B1 + d1×B0 + r1×B-1+ r2×B-2


+ ... + rm-1×B-(m-1) + rm×B-m

Here, B0 = 1

23
Chapter
An Introduction to Computer Science 02

2-2 Binary representation


The most stable and straightforward states of electronic
components in computers are "on" (1) and "off" (0).
Therefore, computers commonly use the binary system
to store data.
Because a byte consists of eight bits, which can be
divided into two hexadecimal digits, computer systems
also commonly use hexadecimal numbers to display
data.

24
Chapter
An Introduction to Computer Science 02

2-2 Binary representation

In the hexadecimal system, the numbers 0 to 15 are


represented respectively by the Arabic numerals 0
to 9 and the letters A to F.
The binary string 11010011 can be represented as
D316or 0xD3 (where '0x' denotes hexadecimal).

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

Simply multiply each binary digit by its corresponding


power of 2 (expressed in decimal).
29
Chapter
An Introduction to Computer Science 02

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

181 divided by 2 equals 90 with


a remainder of 1.→d1is 1
90 divided by 2 equals 45 with a
remainder of 0.→d2 is 0
… Continuing the process.

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.

When converting binary numbers to hexadecimal numbers,


every four binary digits form one hexadecimal digit.

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

2-4 The integer representation is.


The integer representation only represents non-negative
integers. Simply assign the smallest bit string (i.e., the string
of all zeros) to 0, and then represent each subsequent number
in sequence until the largest number.
With n bits, you can represent 2n numbers, and the
represented integer range is from 0 to 2n - 1.
For example, using 8 bits, you can represent all integers from
0 to 28 - 1, which is all integers from 0 to 255.

39
Chapter
An Introduction to Computer Science 02

Unsigned integers.
A correspondence table between binary strings and decimal
numbers.

An 'unsigned integer' represented in 8 bits without a positive or negative sign.

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

Signed magnitude notation


If 8 bits are used, all integers within the range of -(27 - 1) to (27
- 1) can be represented, which is -127 to 127.
The potential issue with this method is:
 There are two representations of zero, namely +0 (000...00)
and -0 (100...00).
 The arithmetic operations involving positive and negative
numbers (such as addition and subtraction) are not
straightforward.
42
Chapter
An Introduction to Computer Science 02

Currently, computers do not use this method to


represent integers.

43
Chapter
An Introduction to Computer Science 02

The 'sign and magnitude notation' represented in 8 bits.

44
Chapter
An Introduction to Computer Science 02

Two's complement representation

The concept of two's complement is that you need


to complement by how much to reach a full set.
If you assume buying items at a supermarket,
totaling 793 yuan, and paying with a 1000 yuan
banknote:

45
Chapter
An Introduction to Computer Science 02

 Set aside the 1000 yuan banknote. Mutter "793" to yourself.


Then, set aside 1 yuan and mutter "794." Set aside another 1
yuan and mutter "795." Adding 5 yuan, mutter "800." Then,
add 100 yuan and mutter "900." Another 100 yuan brings the
total to 1000. Altogether, you've set aside 1 + 1 + 5 + 100 +
100 = 207 yuan, precisely the change you need.
 You're right, you can "make up" the remaining 207 yuan to
reach 1000 yuan with the initial 793 yuan.

46
Chapter
An Introduction to Computer Science 02

ones' complement representation

"In one's complement representation" and "two's


complement representation", the leftmost bit of
the bit string is still used as the sign bit (0 for
positive numbers; 1 for negative numbers); the
remaining n-1 bits are used to represent the
magnitude of the number excluding the sign.

47
Chapter
An Introduction to Computer Science 02

The representation for positive numbers is the


same as in "signed magnitude representation",
while the representation for negative numbers
differs.

48
Chapter
An Introduction to Computer Science 02

One's complement representation

Converting a decimal number to one's complement


representation involves the following steps:
 steps 1:Ignore the symbols first, and convert
the numerical part into binary values.

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

One's complement representation.


One's complement representation also encounters
the 'two zeros' issue. For example, in an eight-bit
system, both 00000000 and 11111111 represent
zero, which can cause calculation problems.

55
Chapter
An Introduction to Computer Science 02

The addition and subtraction operations are also


not straightforward.
So, one's complement representation is not the
method currently used by computers to represent
integers.

56
Chapter
An Introduction to Computer Science 02

Two's complement representation.

The 'two's complement representation' is the


method currently used by computers to represent
integers.

57
Chapter
An Introduction to Computer Science 02

Complement method

The complement method uses the leftmost bit of


the bit string as the sign bit, indicating the sign of
the number (0 for positive, 1 for negative).
The remaining n-1 bits are used to represent the
magnitude of the number excluding the sign.

58
Chapter
An Introduction to Computer Science 02

Two's complement representation.


Converting a decimal number into two's complement
representation involves the following steps:
 steps 1:First, ignore the sign and convert the
numerical part into a binary number.
 steps 2:If the binary number exceeds n-1 bits, it
results in overflow and cannot be converted.
Otherwise, pad the left side of the binary number
with zeros until it has a total of n bits.
59
Chapter
An Introduction to Computer Science 02

 steps 3:If the number is to be converted into a


positive number or zero, the value obtained in
step 2 is the result. If it is a negative number,
keep the rightmost zeros and the rightmost 1
unchanged, and perform the complement
conversion on the remaining bits (original 0
becomes 1; original 1 becomes 0).
60
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

Addition in two's complement

First, align the two's complement bit strings of the


numbers to be added. Then, start adding from the
rightmost bit. If the bits in corresponding positions
add up to two or more, there is a carry.

65
Chapter
An Introduction to Computer Science 02

If there is a carry, propagate it to the left. If there is


a carry in the leftmost bit addition, ignore this carry.
If two positive numbers are added, and the
leftmost sign bit is 1, there is an overflow. If two
negative numbers are added, and the leftmost sign
bit is 0, there is an overflow.

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.

The leftmost bit carries over


when adding, and is ignored.

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.

The leftmost bit does


not generate a carry
when added.

69
Chapter
An Introduction to Computer Science 02

Example16
Adding two negative numbers using the two's complement representation.

The leftmost bit generates a


carry when added, which is
ignored.

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

Two's complement addition


The binary string for 28 is 1 0000 0000.

Conclusion: The numerical value represented by the binary


string of a two's complement negative number -x is 2n - x.

74
Chapter
An Introduction to Computer Science 02

Two's complement addition


Let x and y be two positive numbers. When calculating x + (-y),
it involves adding one positive and one negative number. The
value of -y in two's complement representation is 2n - y.

Scenario • The result of the addition, x - y, should be a positive number.

One • When adding two's complement numbers, we get x + (2n - y) =


2n + (x - y). In this case, the 2n term will cause a carry at the
leftmost position, which is ignored, resulting in x - y.
x>y

75
Chapter
An Introduction to Computer Science 02

Two's complement addition


Scenario • The result of the addition, x - y, should be 0.

Two • When adding two's complement numbers, we get x + (2n - y) = 2n +


(x - y). In this case, the 2n term will cause a carry at the leftmost
position, which is ignored, resulting in x - y = 0.
x=y
• The result of the addition, x - y, should be a negative number, with a
Scenario value of -(y - x).

Three • When adding two's complement numbers, we get x + (2n - y) = 2n +


(y - x). In this case, the 2n term will cause a carry at the leftmost
position, which is ignored. The two's complement representation of
x<y -(y - x) exactly equals 2n - (y - x).

76
Chapter
An Introduction to Computer Science 02

2-5 Floating-point representation


Floating-point representation is the most common
method used by computers to represent real numbers.
"536.87" represented in scientific notation is
"5.3687×102". The operating principle of floating-point
representation is similar; it involves moving the decimal
point to a standard position, allowing it to "float".

77
Chapter
An Introduction to Computer Science 02

In the case of finite-bit numbers, the range of


values that can be represented by floating-point
representation with a floating decimal point is
much larger compared to fixed-point
representation with a fixed decimal point position.

78
Chapter
An Introduction to Computer Science 02

2-5 Floating-point representation


Translate into English: Standardization of Scientific Notation:

10110.100011 1.0110100011×24

 The number to the left of the decimal point must always be 1.


 The sequence 0110100011 to the right of the decimal point is
referred to as the mantissa, while the exponent is 4.

79
Chapter
An Introduction to Computer Science 02

2-5 Floating-point representation


The current adopted floating-point representation is primarily
based on the IEEE 754 standard, which consists of three main
parts:
Floating-point
representation

sign bit Exponent part Mantissa part

80
Chapter
An Introduction to Computer Science 02

2-5 Floating-point representation

Single-precision floating-point number :1 bit for


sign; 8 bits for exponent; 23 bits for mantissa.
Double-precision floating-point number : 1 bit for
sign; 11 bits for exponent; 52 bits for mantissa.

81
Chapter
An Introduction to Computer Science 02

82
Chapter
An Introduction to Computer Science 02

Single-precision floating-point number


Sign bit :1 bit, where 0 represents positive and 1
represents negative.
Exponent part :8 bits, using the Excess 127
representation (subtracting 127 from the binary value to
obtain the actual stored value). The range of values
stored in 8 bits is from 0 to 255, providing a total of 28 =
256 possible variations.

83
Chapter
An Introduction to Computer Science 02

Mantissa part :23 bits, starting from the


normalized decimal point, with trailing zeros added
if necessary.

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

The largest negative number


10000000100000000000000000000000,
Its value is -2-126.
The smallest negative number
11111111011111111111111111111111,
Its value is -(2 - 2-23) × 2127.

90
Chapter
An Introduction to Computer Science 02

2-6 ASCII and Unicode

The ASCII (pronounced as "Ass-key"; American


Standard Code for Information Interchange)
published by the United States National Standards
Institute in 1963 is the most widely used standard
today.

91
Chapter
An Introduction to Computer Science 02

2-6 ASCII and Unicode

Standard ASCII uses 7 bits to store one character,


resulting in 27 = 128 possible combinations. The
most common storage unit in computers is 8 bits,
with the extra bit often used for storing a parity bit
for error checking.

92
Chapter
An Introduction to Computer Science 02

2-6 ASCII and Unicode

Extended ASCII uses 8 bits to store one character,


resulting in 28 = 256 possible combinations. This
allows for the storage of non-English characters,
graphical symbols, mathematical symbols, and
more.

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

The first 128 characters of Unicode


are ASCII characters, while the rest
represent commonly used text in
English, Chinese, Japanese, Korean,
and other non-English speaking
countries.

96
Chapter
An Introduction to Computer Science 02

The largest category in Unicode is CJK, which


primarily consists of the character sets for Chinese,
Japanese, and Korean, including their respective
Han characters.

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

These fonts have gradually been incorporated into


the Unicode CJK character set, making future
integration and standardization imminent.

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.

Figure 4.2 Logic operators applied to bit patterns

106
Example 4.3

Use the NOT operator on the bit pattern 10011000.

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

Use the OR operator on the bit patterns 10011001 and 00101110.

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

The four logic operations can be used to modify a bit


pattern.

 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

You might also like