Ch02 Data and Number Representations 1127
Ch02 Data and Number Representations 1127
Liuying Wen
School of Computer Science
Email : [email protected]
Outline
Data Representation
Data and information
Data Types
Digital Data Representation
How to Represent Different Types of Data
Number Representation
Number Systems
Conversion Between Number Systems
Integer Representation
Operations On Bits
Arithmetic Operations
Logical Operations
2
Objectives
Differentiate between data and information.
Explain how text, image, audio are represented
in computers.
Explain decimal notation, binary notion,
hexadecimal notation, and octal notation.
Apply conversions from one number system to
another.
3
Objectives
4
Part 1
Data Representation
5
Overview
What’s data?
Data refers to the symbols that a computer uses to
represent facts(such as people, events and things and idea).
What’s information?
The words, numbers, and graphics used as the basis for
human actions and decisions.
The difference between data and information:
Data becomes information when it is presented in a format
that people can understand and use.
6
Data VS Information
Data Information
7
Overview(con.)
8
Overview(con.)
There are different types of data
Data
9
Question: How do you handle all these data
types(text, number, image, audio and
video)?
10
Solution
11
Digital Data Representation
Digital device works with discrete, distinct data or
digits, such as 1 and 0
Analog device works with continuous data.
15
Text Representation
A piece of text in any language is a sequence of
symbols.
Symbol examples:
26 uppercase letters(A,B,C,…,Z)
26 lowercase letters(a, b, c, … , z)
10 numeric character(0,1,2,…,9)
Others(?;blank, newline, and table…)
In a bit pattern, the number of bits to represent a
symbol depends on how many symbols are in the set.
More symbol mean a longer bit pattern.
16
Text Representation(con.)
17
Text Representation(con.)
The relationship between the length of the bit pattern
and number of symbols is logarithmic.
For example:
If you need 4 symbols, the length is 2 bits.
Log2 4=2 The forms are: 00, 01, 10 and 11.
18
Text Representation(con.)
Different sets of bit patterns have been designed
to represent text symbols.
Each set is called a code, or code scheme.
There are some common codes:
ASCII
Extended ASCII
EBCDIC
Unicode
ISO
19
Popular Code Schemes
ASCII Code
Stands for American Standard Code for
Information Interchange.
It was developed by ANSI(American National
Standards Institute).
It is used on most microcomputers, many
minicomputers, and some mainframe computers.
This code uses 7 bits for each symbol, which mean
128(27)different symbols can be defined.
20
Popular Code Schemes
Features of ASCII Code
Range of 7-bit pattern is from 0000000 to 1111111.
The first pattern 0000000 represents the null
character.
The last pattern 1111111 represents the delete
character.
There are 31 control characters.
……
21
22
Popular Code Schemes
Extended ASCII Code
To make the size of each pattern 1 byte(8 bits), the
ASCII bit patterns are augmented with an extra 0
at the left.
The code is from 00000000 to 01111111.
The left bit (0) is extended bit.
23
Image Representation
Image
Bitmap Vector
Bitmap
An image is divided into a matrix of pixels.
Each pixel is assigned a bit pattern.
To represent a black an white image, 1 represents white
pixel and 0 represents black one.
To represent a color image, a pixel is decomposed into
three colors(RGB)
24
Image Representation(con.)
25
26
Image Representation(con.)
27
Image Representation(con.)
Vector
An image is decomposed into a combination of curves
and lines.
Each curve or line is represented by a mathematical
formula.
For example, a line may be described by the
coordinates of its endpoints, and a circle may be
described by the coordinates of its center and the
length of its radius.
They use much less storage space than bitmap images,
but do not look as realistic as bitmap images.
28
29
vector graphic----
a set of instructions that recreates a picture
draw circle
center 0.5, 0.5
radius 0.4
fill-color yellow
stroke-color black
stroke-width 0.05
draw circle
center 0.35, 0.4
radius 0.05
fill-color black
draw circle
center 0.65, 0.4
radius 0.05
fill-color black
draw line
start 0.3, 0.6
end 0.7, 0.6
stroke-color black
stroke-width 0.1
Audio Representation
31
Audio Representation(con.)
32
Audio Representation(con.)
33
Part 2
Number Representation
34
Some essential concepts
Number system: any system of naming or
representing numbers, also called number
representation system, or numeration.
Base/ Radix : the number of digits in a number
system.
Carry: happens when the sum or product of two
or more digits equals or exceeds the base of the
number system
Position value: the value associated with each
digit place, also called weight or positional value.
35
For example: for the decimal number 263,
the position values associated with the
character 2, 6, 3 are respectively 102, 101,
100, therefor,
36
37
Different Number System
Number
Base Digits(Symbols)
System
Decimal 10 0,1,2,3,4,5,6,7,8,9
Binary 2 0,1
Octal 8 0,1,2,3,4,5,6,7
Hexadecimal 16 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F
38
Question: How do we tell which number
system we are using?
Subscript method:
(101)2 Binary 101B
(101)8 Octal 101O
(101)10 Decimal 101D
(101)16 Hexadecimal 101H
Prefix method:
Postfix method:
39
1. Decimal System
Nowadays , there are two dominant
number systems in the world: decimal system
and binary system.
It has ten digits: 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9.
40
2. Binary System
41
2. Binary System
42
Binary Position Value Table
43
How computer capacity is
expressed: bit by bit
The following terms are used to denote
capacity:
Bit: In the binary system, the binary digit(bit)-0
or 1-is the smallest unit of measurement.
Byte: A group of 8 bits is called a byte, and a byte
represents one character, digit, or other value.
Kilobyte: A kilobyte(K, KB) is about 1000 bytes.
(Actually, it’s precisely 1024, that is 210 bytes)
44
How computer capacity is
expressed: bit by bit
Megabyte: A megabyte(M, MB) is about 1 million
bytes(220).
Gigabyte: A gigabyte(G, GB) is about 1 billion
bytes(230).
Terabyte: A terabyte(T, TB) represents about 1
trillion bytes(240).
Petabyte(250): A new measurement accommodates
the huge storage capacities of modern database-a
petabyte represents about 1 million gigabytes!
45
How computer capacity is
expressed: bit by bit
1 byte = 8 bit
1KB (KiloByte) = 210 B
1MB (MegaByte)= 210 KB
1GB (GigaByte)= 210 MB
1TB (Terabyte) = 210 GB
46
3. Octal System
Its base is 8.
It has eight digits: 0, 1, 2, 3, 4, 5, 6 and 7.
A 3-bit pattern can be represented by an
octal digit, and vice versa.
47
4. Hexadecimal System
Its base is 16..
It has sixteen digits : 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
A, B, C, D, E and F.
A 4-bit pattern can be represented by a
hexadecimal digit, and vice versa
48
49
Conversion between number
systems
50
Binary to Decimal Conversion
51
Binary to Decimal
Conversion(con.)
Binary Number 0 1 0 1 1 0 1
Position 6 5 4 3 2 1 0
Position value 26 25 24 23 22 21 20
0× 26 + 1 × 25 + 0 × 24 + 1 ×23 + 1 × 22 + 0 × 21 + 1 ×20
Result 0 + 32 + 0 + 8 + 4 + 0 + 1 = (45)10
52
Decimal to Binary Conversion
53
Example: convert the decimal
number 35 to binary.
Stop when the
quotient is 0 Quotient
0 1 2 4 8 17 35
1 0 0 0 1 1
Remainder
Binary Number: 100011
54
Questions:
55
Binary to Octal Conversion
56
Binary to Octal Conversion(con.)
1 1 1 1 1 1 0 0 1 1 1 0 0 1 0 0
1 7 6 3 4 4
58
Binary to Hexadecimal Conversion
59
Binary to Hexadecimal Conversion(con.)
1 1 1 1 1 1 0 0 1 1 1 0 0 1 0 0
F C E 4
61
Exercises
(A7B8.C9)16 = (?) 2
Integers
Unsigned Signed
63
1. Unsigned Integers
64
65
2. Signed Integers
Sign-and-Magnitude Representation
The leftmost bit defines the sign of the number: 0 for positive,
and 1 for negative.
For example: in a 8-bit allocation, the leftmost bit shows the
sign and the other seven bits represent the absolute value.
Range of signed integers: -(2N-1-1) ~ + (2N-1-1)
N is the number of bits allocated to represent one sign-and-
magnitude integer.
66
Sign-and-Magnitude Representation
67
Example: Store +7 in an 8-bit memory
location, and store -258 in a 16-bit memory
location using sign-and-magnitude
representation.
+7
1 1 1
0 0 0 0 1 1 1
0 0 0 0 0 1 1 1
-258
1 0 0 0 0 0 0 1 0
0 0 0 0 0 0 1 0 0 0 0 0 0 1 0
1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0
68
2. Signed Integers
One’s Complement Representation
The one’s complement of a positive binary
number is the same as the original binary
number.
To obtain one’s complement of a negative
binary number, you simply need to flip all the
bits(0s to 1s and 1s to 0s)----except for the
sign-bit.
69
One’s Complement Representation
70
Example: Store +7 and -7 in an 8-bit memory
location, and store -258 in a 16-bit memory
location using one’s complement representation.
+7 -7
1 1 1 1 1 1
0 0 0 0 0 1 1 1 0 0 0 0 0 1 1 1
1 1 1 1 1 0 0 0
-258
1 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0
1 1 1 1 1 1 1 0 1 1 1 1 1 1 0 1
71
2. Signed Integers
72
Two’s Complement Representation
+7
For negative numbers:
1 1 1
Two’s complement = one’s complement + 1
0 0 0 0 0 1 1 1
-40
1 0 1 0 0 0
0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0
1 1 1 1 1 1 1 1 1 1 0 1 0 1 1 1
1 1 1 1 1 1 1 1 1 1 0 1 1 0 0 0
74
Floating-point Representation
Integer Part 14 . 234 Fraction Part
75
Changing fractions to binary
Multiply by 2 to Stop when
get integer part the result is 0
0 0 0 1
Integer
part
Binary Number: 0.0001
76
Excise: transform the number 49.58 to a
binary(3 bits after the decimal point)?
77
Part 3
Operations on Bits
78
Bit Operations
79
Arithmetic Operations
80
Arithmetic Operations
81
Example: (10111)2+ (00011)2=?
10111
+) 00011
11010
82
Question: How does the
computer subtract two integers?
For example: 5 – 2 = ?
1. Transform subtracting to adding.
2. Use the two’s complement representation.
83
Example: subtract the following numbers in
two’s complement representation:
(5)10- (2)10=?
+ 1 1 1 0
0 0 1 1 3
84
Exercises
85
Logical Operations
It’s a cat, not a tiger!
True
It’s a red rose!
False
Each logical variable can only be True or False.
In the computer system, “1” means “True”, “0”
means “False”.
86
Some essential concepts
87
Some essential concepts
88
NOT operator
89
Example: Use the NOT operator on bit pattern
10011000
NOT 10011000
01100111
90
AND operator
91
Example: Use the AND operator on bit pattern
10011000 and 00110101
10011000
AND 00110101
00010000
92
Inherent rule of AND operator
93
OR operator
94
Example: Use the OR operator on bit pattern
10011000 and 00110101
10011000
OR 00110101
10111101
95
Inherent rule of OR operator
96
XOR operator
97
Example: Use the XOR operator on bit pattern
10011000 and 00110101
10011000
XOR 00110101
10101101
98
Inherent rule of XOR operator
99
Objectives
Differentiate between data and information.
Explain how text, image, audio and video are
represented in computers.
Explain decimal notation, binary notion,
hexadecimal notation, and octal notation.
Apply conversions from one number system to
another.
100
Objectives
101
That’s all for this
chapter!
102