Lesson 2 - Data in Computer
Lesson 2 - Data in Computer
Lesson 2
Representation of Data in computer
• Numeral system:
– Is a set of symbols and rules, using which we
can represent and determine the value of
numbers.
– Each numeral system uses a finite set of
characters/symbols/digits. The number of
characters/symbols/digits is called the base or
radix of the numeral system, normally
denoted by b
– E.g.: In the decimal system, we use ten digits:
0,1,2,…,9, hence b =10
3
I. Numeral systems
5
Decimal system
6
Decimal system
7
b-base system
8
b-base system
9
The binary system
• Base b = 2
• Using 2 digits: 0 and 1
• Binary digit (0 or 1) is called a bit
(binary digit)
E.g.: bit 0, bit 1
• Bit is the smallest information unit
10
The binary system
11
The binary system
12
The binary system
13
Plus/Minis in the binary system
• Plus:
1+0=0+1=1;
0+0=0;
1+1=10;
• Minus
1-1=0;
0-0=0;
1-0=1;
0-1=1; (borrow 1)
14
Example: Plus
1 0 1
+11 1
---------
11 0 0
15
Example: Minus
1 1 0 0
- 1 1 1
--------------------
01 0 1
16
The octal system
• Base b = 8
• Using 8 digits: 0,1,2,3,4,5,6,7
• With n digits we can represent 8n
different values, ranging from 0 to 8n
-1 as:
00...000 = 0
...
77...777 = 8n -1
17
The octal system
with
i m b = 8.
18
The octal system
19
The hexadecimal system
• Base b = 16
• Using 16 digits and
characters:
0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F
in which characters:
A, B, C, D, E, F
represent respected
number in decimal
system
10, 11, 12, 13, 14, 15.
20
The hexadecimal system
21
The hexadecimal system
• E.g.: Determine value of 34F5C.12D(16) :
34F5C.12D(16) =
3x164 + 4x163 + 15x162 + 5x161 + 12x160
+? = 216294(10) +?
?=1/16 + 2/16/16 +13/16/16/16=0.07348..
22
Convert a number from decimal system to
other numeral system
• In general a number N in the decimal
system (N(10)) consists of 2 part: Integer
and Fraction.
• Converting a number from decimal system
to other numeral system including 2 steps:
– Convert integer part from decimal system to
b-base system
– Convert fraction part from decimal system to
b-base system
23
Convert a number from decimal system to
other numeral system
• Convert integer part:
– Step 1: Divide the integer part of N(10) by b,
we obtain T1, remainder is d1.
– Step 2: if T1 <> 0, Divide T1 by b, we obtain T2,
remainder is d2
….
– Step n: Tn = 0, remainder is dn => stop
– Result number N(b) obtained by remainders
in above steps as:
N(10) = dndn-1…d1 (b)
24
Convert a number from decimal system to
other numeral system
– E.g.: Convert integer part of number
12.6875(10) to binary system:
25
Convert a number from decimal system to
other numeral system
• Convert fraction part of a number in decimal
system to other system:
– Step 1: Multiple fraction part of N(10) with b,
we obtain a number in form of x1.y1 (in which x
in integer, y is fraction)
– Step 2: If y1 <> 0, multipe 0.y1 with b, we
obtain x2.y2
– Step n: If yn-1 <> 0, multiple 0.yn-1 with b, we
obtain xn.0, => stop
– Result number is:
0,x1x2…xn
26
Convert a number from decimal system to
other numeral system
– E.g.: Convert fraction part of number
12.6875(10) to binary system:
27
Examples
1) Decimal Binary 124.75 = ?
2) Decimal Binary 65.125 = ?
3) Binary Hexa: 1011 1110 0110 = ?
4) Hexa Binary: 3E8 = ?
5) Hexa Decimal: 3A8C = ?
28
Represent data in computer
• All data using in computer must be
encoded into binary numbers
• Data in computer is classified into 2 types:
– Basic data
– Structured data
29
Basic data
• Integer
– Unsigned integer: Using binary.
– Signed integer: Using the 2's complement
encoding.
• Real:
– Using floating point number
• Character:
– Using character encode: ASCII, Unicode, …
30
Structured data
31
Information Unit
32
Represent data in computer
33
Representation of integer number
34
Representation of unsigned integer
n 1
A ai 2i
i 0
B = 156 = 128 + 16 + 8 + 4 = 27 + 24 + 23 + 22
B = 1001 1100
Examples
X = 0010 1011 = 25 + 23 + 21 + 20
= 32 + 8 + 2 + 1 = 43
Y = 1001 0110 = 27 + 24 + 22 + 21
= 128 + 16 + 4 + 2 = 150
Representation of signed integer
• Given representation of signed integer A as:
an-1an-2...a2a1a0
then, value of A is determined as:
n2
A an 1 2 n 1
ai 2 i
i 0
• Using n bits, values of A is in: [-2n-1, 2n-1-1]
10000…000
……….
01111…111
38
Representation of signed integer
• Given representation of signed integer A as:
an-1an-2...a2a1a0
• If an-1:= 0:
• A is positive number
• an-2...a2a1a0 represent value of A
n2
A a 2i 0
i
i
i 0
40
Representation of signed integer
• E.g.: Determine value of signed integers A and
B:
A = 0101 0110
B = 1101 0010
A = 26 + 24 + 22 + 21 = 64 + 16 + 4 + 2 = +86
B = -27 + 26 + 24 + 21 =
= -128 + 64 + 16 + 2 = -46
41
Representation of signed integer
42
Representation of signed integer
• Using the 2’s complement encoding
• Given an unsigned integer A which is
represented in the binary system using n
bits, then:
– The 1’s complement encoding of A = (2n - 1) - A
– The 2’s complement encoding of A = 2n – A
• E.g.:
• given A = 0110
• The 1’s complement encoding of A = (24 - 1) - 0110 =
1001
• The 2’s complement encoding of A = 24 - 0110 = 1010
43
Representation of signed integer
• A signed integer number –A is represented by
the 2’s complement encoding of A
• E.g.: Represent signed number A = -70 using 8
bits
A = 70 = 0100 0110
1’s comp.: 1011 1001
+ 1
2’s comp.: 1011 1010
Hence: -70 => 1011 1010
44
Operations on integer number
45
Examples
46
Plus/minus signed integer
• Sum: plus bit by bit from right side to left
side, remove remember bit if has.
• Minus:
A – B = A + ( - B) = A + the 2’s complement
encoding of B
47
Plus/minus signed integer
• Sum of two signed integers with opposite sign:
Always return correct result
• Sum of two signed integers with same sign:
• If sign of Sum is the same as operands, then
result is correct
• If not, result is wrong because of overflow
• Overflow is occurred if the result is out of range
of representing integer using n bit:
[-2n-1, 2n-1-1]
48
Examples
• No overflow
49
Examples
• Overflow
50
Multiple/divide unsigned integer
• E.g.: Divide
52
Logic operations on a bit
AND OR XOR
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0
53
Logic operations on a bit
NOT
0 1
1 0
54
Logic operations on a binary number
– Perform logic operations on each
couple of bits of two operands
– These logic operands are only affect on
that couple of bits, not on others.
55
Examples
• VD: A = 1010 1010 và B = 0000 111
AND OR XOR NOT
56
Representation of Real number
• General rule:
– In order to represent real number in
computer, we use Floating point number
57
Representation of Real number
• A floating point number has the form of:
X = M * RE
in which:
– M is mantissa
– R is radix (base), usually be 2 or 10.
– E is exponent
• Given R, to represent a real number X, we
need to determine M and E (as integer
number)
58
Representation of Real number
• E.g.: Using R = 10, given two real
numbers N1 and N2 represented in
floating point with:
– M1 = -15 và E1 = +12
– M2 = +314 và E2 = -2
59
Operations on floating point real number
• Given two floating point real numbers:
– N1 = M1 x RE1 and N2 = M2 x RE2
• Add/Subtract:
N1 ± N2 = (M1 x R E1-E2 ± M2) x RE2 ,
• Multiple and divide
N1 x N2 = (M1x M2) x R E1+E2
N1 /N2 = (M1 / M2) / R E1-E2
60
IEEE 754/85 format
61
IEEE 754/85 format
Encoding format
31 30 23 22 0
S e m
63 62 52 51 0
S e m
79 78 64 63 0
S e m
62
IEEE 754/85 format
• S is signed bit: S=0 for positive and and
S=1 for negative number.
• e is excess of exponent E: E = e – b
where b is bias:
– With 32-bit : b = 127, then E = e - 127
– With 64-bit : b = 1023, then E = e - 1023
– With 80-bit : b = 16383, then E = e – 16383
• m is fraction part of M: M = 1.m
63
IEEE 754/85 format
S e m
64
Example
• E.g.1: Given X which is represented in
IEEE 754 32 bit:
1100 0001 0101 0110 0000 0000 0000 0000
Determine value of X.
• E.g.2: Given Y which is represented in
IEEE 754 32 bit:
0011 1111 1000 0000 0000 0000 0000 0000
Determine value of Y.
65
Example
• E.g. 3: Given a real number X = 6.375.
Represent X in IEEE 754 using 32 bit
66
Note
• If all bits of e are 0 and all bits of m are 0,
then X = 0
• If all bits of e are 1 and all bits of m are 0,
then X =
• If all bits of e are 1 and at least one bit of
m is 1, the X is not a number (NaN)
67
Representation range
underflow
overflow overflow
-¥ -b -a -0 +0 a b +¥
68
Operations of IEEE
• X1 = M1 * RE1
• X2 = M2 * RE2
• Ta có
– X1 X2 = (M1 * RE1-E2 M2) * RE2 , với E1
E2
– X1 * X2 = (M1 * M2) * RE1+E2
– X1 / X2 = (M1 / M2) * RE1-E2
69
Representation of characters
• General rule:
– Convert characters to binary.
– The number of bits used to encode each
character depend on encoding set.
– E.g. :
• ASCII uses 8 bits
• Unicode uses 16 bits.
70
ASCII
71
72
ASCII
• 95 displayable characters has code:2016 ÷
7E16, in which:
– 'A' ÷ 'Z' has code: 4116 ÷ 5A16
– 'a' ÷ 'z' has code: 6116 ÷ 7A16
– '0' ÷ '9' has code: 3016 ÷ 3916
73
ASCII
• 33 control characters has code: 0016 ÷ 1F16
and code 7F16 is used for control function
74
Format control
BS Backspace - Lùi lại một vị trí: Ký tự điều khiển con trỏ lùi lại một vị trí.
HT Horizontal Tab - Tab ngang: Ký tự điều khiển con trỏ dịch tiếp một khoảng đã
định trước.
LF Line Feed - Xuống một dòng: Ký tự điều khiển con trỏ chuyển xuống dòng
dưới.
VT Vertical Tab - Tab đứng: Ký tự điều khiển con trỏ chuyển qua một số dòng đã
định trước.
FF Form Feed - Đẩy sang đầu trang: Ký tự điều khiển con trỏ di chuyển xuống
đầu trang tiếp theo.
CR Carriage Return - Về đầu dòng: Ký tự điều khiển con trỏ di chuyển về đầu
dòng hiện hành.
75
Data transfer control
SOH Start of Heading - Bắt đầu tiêu đề: Ký tự đánh dấu bắt đầu phần thông tin tiêu
đề.
STX Start of Text - Bắt đầu văn bản: Ký tự đánh dấu bắt đầu khối dữ liệu văn bản
và cũng chính là để kết thúc phần thông tin tiêu đề.
ETX End of Text - Kết thúc văn bản: Ký tự đánh dấu kết thúc khối dữ liệu văn bản
đã được bắt đầu bằng STX.
EOT End of Transmission - Kết thúc truyền: Chỉ ra cho bên thu biết kết thúc
truyền.
ENQ Enquiry - Hỏi: Tín hiệu yêu cầu đáp ứng từ một máy ở xa.
ACK Acknowledge - Báo nhận: Ký tự được phát ra từ phía thu báo cho phía phát
biết rằng dữ liệu đã được nhận thành công.
NAK Negative Aknowledge - Báo phủ nhận: Ký tự được phát ra từ phía thu báo cho
phía phát biết rằng việc nhận dữ liệu không thành công.
SYN Synchronous / Idle - Đồng bộ hóa: Được sử dụng bởi hệ thống truyền đồng bộ
để đồng bộ hoá quá trình truyền dữ liệu.
ETB End of Transmission Block - Kết thúc khối truyền: Chỉ ra kết thúc khối dữ
liệu được truyền.
76
Separate information control
FS File Separator - Ký hiệu phân cách tập tin: Đánh dấu ranh giới giữa các tập tin.
GS Group Separator - Ký hiệu phân cách nhóm: Đánh dấu ranh giới giữa các
nhóm tin (tập hợp các bản ghi).
RS Record Separator - Ký hiệu phân cách bản ghi: Đánh dấu ranh giới giữa các
bản ghi.
US Unit Separator - Ký hiệu phân cách đơn vị: Đánh dấu ranh giới giữa các phần
của bản ghi.
77
Other controls
NUL Null - Ký tự rỗng: Được sử dụng để điền khoảng trống khi không có dữ liệu.
BEL Bell - Chuông: Được sử dụng phát ra tiếng bíp khi cần gọi sự chú ý của con người.
SO Shift Out - Dịch ra: Chỉ ra rằng các mã tiếp theo sẽ nằm ngoài tập ký tự chuẩn cho
đến khi gặp ký tự SI.
SI Shift In - Dịch vào: Chỉ ra rằng các mã tiếp theo sẽ nằm trong tập ký tự chuẩn.
DLE Data Link Escape - Thoát liên kết dữ liệu: Ký tự sẽ thay đổi ý nghĩa của một hoặc
nhiều ký tự liên tiếp sau đó.
DC1 ÷ Device Control - Điều khiển thiết bị : Các ký tự dùng để điều khiển các thiết bị
DC4 phụ trợ.
CAN Cancel - Hủy bỏ: Chỉ ra rằng một số ký tự nằm trước nó cần phải bỏ qua.
EM End of Medium - Kết thúc phương tiện: Chỉ ra ký tự ngay trước nó là ký tự cuối
cùng có tác dụng với phương tiện vật lý.
SUB Substitute - Thay thế: Được thay thế cho ký tự nào được xác định là bị lỗi.
ESC Escape - Thoát: Ký tự được dùng để cung cấp các mã mở rộng bằng cách kết hợp
với ký tự sau đó.
DEL Delete - Xóa: Dùng để xóa các ký tự không mong muốn.
78