Tin học đại cương - Unit 1 (part 2)
Tin học đại cương - Unit 1 (part 2)
Representation of Information
in Computers
Contents
2.1. Number Systems
2.2. Number Representation in Computers - Units of
Information
2.3. Representation of Integers
2.4. Operations on Integers
2.5. Logical operations on Binary Numbers
2.6. Symbol Representation
2
2.1. Number Systems
3
Number Categories
• Natural numbers
• Integers
• Rational numbers
• Irrational numbers
4
Decimal System
5
Decimal System (cont’d)
If i is an integer written in decimal form with digits dj:
i = dn-1dn-2 . . . . d1d0
then i represents the sum:
n −1
j
d
j =0
* 10 j
6
Rational/Irrational numbers
• dn-1dn-2 . . . . d1d0.d-1d-2…..d-m
𝑛−1
𝑑𝑗 ∗ 10𝑗
𝑗=−𝑚
• 8943043.456
7
Base-b System
where n is the total number of digits, and dj is the j th digit from the
rightmost position in the decimal number.
• When the base is higher than 10, we need symbols to represent the
digits that correspondent to the decimal values of 10 and beyond.
8
Example
Convert 11101.112 to Decimal
9
More examples
• 110010.10012 = 32 + 16 + 2 + 0.5 + 0.0625 = 50.562510
• 456718=4*4096+5*512+6*64+7*8+1=1938510
• 14516=1*256+4*16+5=325
10
Convert integers from decimal to base b
Remainder Method:
• Let value = (dn-1 dn-2 … d2 d1 d0)10.
• First divide value by b, the remainder is the least
significant digit b0.
• Divide the result by b, the remainder is b1.
• Continue this process until the result is less than b, giving
the most significant digit, bm-1.
• The result is bm-1 bm-2 … b2 b1 b0
11
Example 1
Use Remainder Method to convert 12 (base 10) to base 2
12
More examples
• 63 = 1111112
• 20 = 101002
• 81 = 10100012
• 126 = 11111102
• 49=1111111102
• 72= 111111117
• 68=
13
Convert Decimal Fractions to Base-b
• Begin with the decimal fraction and multiply by b. The
whole number part of the result is the first digit to the
right of the point.
14
Example 1
Convert the decimal value .625 to a binary representation
• 0.625 x 2 = 1.25
The first binary digit to the right of the point is a 1.
• 0.25 x 2 = 0.50,
The second binary digit to the right of the point is a 0.
• 0.50 x 2 = 1.00
The third binary digit to the right of the point is a 1.
• We had 0 as the fractional part of our result,
15
Example 2
Binary representation of the decimal fraction 0.1
1. .1 x 2 = 0.2
The first binary digit to the right of the point is a 0
2. .2 x 2 = 0.4
The second binary digit to the right of the point is also a 0.
3. .4 x 2 = 0.8
The third binary digit to the right of the point is also a 0.
4. .8 x 2 = 1.6
The fourth binary digit to the right of the point is a 1.
5. .6 x 2 = 1.2
The fifth binary digit to the right of the point is a 1.
6. The next step to be performed (multiply 2. x 2) is exactly the same action
we had in step 2. We are then bound to repeat steps 2-5, then return to
Step 2 again indefinitely
.1 (decimal) = .00011001100110011 . . .
The repeating pattern is 0011
16
More examples
• 67.25 = 1000011.012
• 22.75 = 10110.112
• 72.625 = 1001000.1012
• 15.6 = 1111.100110011001…..2
• 1,2,4,8,16,32,64,128,256,512,1024
• 80 = 64+16=2^6 + 2^4 = 1010000
• 127 = 128 – 1 = 10000000 – 1 = 1111111
• 125 = 127 – 2^1 = 1111101
• 67 = 64 + 2+ 1=2^6+2^1+2^0 = 1000011
17
•1+0=1
•0+0=0
• 1 + 1 = 10
• 1 + 1 + 1 = 11
• 1 + 1 +1 +1 = 100
•1–0=1
•0–0=0
• 0 – 1 = 1 (vay 1)
18
Binary System
• All data, including programs, in a computer system is
represented in terms of groups of binary digits
• A single bit can represent one of two values, 0 or 1.
• If we have several symbols to represent, we can make a
one-to-one correspondence between the patterns and the
symbols.
• Example : 0, 1, 2, 3 are mapped to the patterns 00,
01, 10, 11
• A group of k binary digits (bits) can be used to represent 2k
symbols
19
Hexadecimal System
20
Octal System
• The octals numbers include only the representations for
the values from 0 to 7: 0, 1, 2, 3, 4, 5, 6, 7
• Example:
235 . 648
= 2x82 + 3x81 + 5x80 + 6x8-1 + 4x8-2
= 157. 812510
21
• A416= 1010 01002
• 1010 11012 = AD16
• 11011111102=37E16
22
Data Representation in a Computer
Units of Information
• Basic Principles
• Units of Information
23
Basic Principles
• Data can be numbers, symbols, images, sounds . . .
• To store in computers, it’s necessary to represent
data in term of bit patterns
• There are different ways to encode different types of
data
• Numbers are converted to their binary
representations following some standards.
• Symbols are assigned to a bit pattern
• Other data must be digitalized.
24
Data and Computers
• Every task, a computer manages data in some ways
• In the past, computers dealt exclusively with numeric and
textual data
• At present, computers are multimedia devices -> deal with
different types of data:
• Numbers
• Text
• Audio
• Images and Graphics
• Video
• All of data is stored as binary digits: strings of 1s and 0s
25
2.2. Number Representation in
Computers - Units of Information
26
Analog and Digital Information
• Most part of the natural world is continuous and
infinite
• Computers are finite
• Information can be represented in analog or digital
27
Binary Representation
28
Representing Audio Information
• One binary digit can be either 0 or 1
• To represent more than 2 things, we need more
binary digits
• N binary digits can represent 2n things
29
Representing Colors
• Often expressed as an RGB (red – green – blue)
value
• Three numbers indicate the contribution of each of
three primary colors
• Amount of data that used to represent a color is
called color depth
• HiColor: 16 bit color depth
• TrueColor: 24bit color depth
30
Three Dimensional Color Space
31
Some TrueColor RGB Values
32
Digitized Images and Graphics
• Image is a collection of individual dots called pixel
• Each pixel is composed of a single color
• The number of pixel used to represent a picture is
called resolution
• 640 x 480, 1270 x 480, 1920 x 1024
33
Categorization of Data Types
• Basic types : the standard scalar predefined types that one
would expect to find ready for immediate use in any
imperative programming language
• Structured types: made up from such basic types or other
existing higher level types.
34
Units of information
• The smallest unit of information a computer can use
is bit (Binary Digit)
• The difference between two states (high current and
low current) is represented as one of two numbers (1
or 0).
• A collection of 8 bits are put together to form a byte
• Binary prefixes can be used to quantify computer
memory sizes
• Each successive prefix is multiplied by 1024 (210)
rather than the 1000 (103)
35
Units of information
36
Representation of Integers
• Unsigned Integers
• Signed Integers
37
Unsigned Integers
38
Signed Integers
• The most significant bit is set to 0 and 1 for positive and
negative numbers
• Example
+4210 = 001010102 = 32 + 8 + 2
and so
-4210 = 110101102
• All number whose leftmost bit is 1 is considered negative
39
Signed Integers (cont’d)
• If we have 4 bits in our representation then,
• The most positive representable number using sign-and-
magnitude is 0111
• The most negative representable number using sign-and-
magnitude is 1000
• How to represent negative number?
40
Two’s Complement
• Representation for signed binary numbers
• Leading bit is a sign bit
• Binary number with leading 0 is positive
• Binary number with leading 1 is negative
• Magnitude of positive numbers is just the binary representation
• Magnitude of negative numbers is found by
• Complement the bits: replace all the 1's with 0's, and all the 0's with 1’s –
one’s complement
• Add one to the complemented number
• The carry in the most significant bit position is thrown away when
performing arithmetic
41
Example
Performing two's complement on the decimal 42 to get -42
• Using a eight-bit representation
42
Two's Complement Arithmetic
• Computing 50 - 42 using a two's complement
representation with eight-bit numbers
50 - 42 = 50 + (-42) = 8
00110010 50
44
Two's Complement Arithmetic
i =0
• Range: [-2n-1, 2n-1-1]
10000…000
……….
01111…111
• Negative – positive numbers?
45
With n = 8 bit
Exercise 1
• Present the following integer values as signed 8-bit integer in
computers
• 30 = 16 + 8 + 4 + 2 = 0001 1110
• -50: 50 = 32 + 16 +2 = 0011 0010; -50 = 1100 1101 + 1 = 1100 1110
• 77 = 64 + 8 +2 +1 = 0100 1011
• -91:91=64+16+8+2+1= 0101 1011;-91 = 1010 0100 + 1 = 1010 0101
• 130=128 + 2 = 1000 0010
47
Operations on Integers
• Addition and Subtraction
48
Addition and Subtraction
• Addition
• Subtraction
49
Binary Addition
0+0=0
0+1=1
1+0=1
1 + 1 = 0, and carry 1 to the next more significant bit
50
Example
00011010 + 00001100 = 00100110
1 1 carries
0 0 0 1 1 0 1 0 = 26 (base 10)
+ 0 0 0 0 1 1 0 0 = 12 (base 10)
0 0 1 0 0 1 1 0 = 38 (base 10)
51
Binary Subtraction
0-0=0
0 - 1 = 1, and borrow 1 from the next more significant bit
1-0=1
1-1=0
52
Example: Addition and substraction
• No overflow
53
Example: Addition and substraction
• Overflow
Substraction??
54
Copyright by SOICT
Exercise 2
• Calculate the following mathematical expression if
they are represented as signed 8-bit integers.
• 30-90= 11000100 = -60
• -50+70=00010100 = 20
• 77+60=10001001 = -119
• -91-51=01110010 = 114
55
Multiplication and Division
• Multiplication
• Division
56
Binary Multiplication
0x0=0
0x1=0
1x0=0
1 x 1 = 1, and no carry or borrow bits
57
Example
00101001 × 00000110 = 11110110
0 0 1 0 1 0 0 1 = 41(base 10)
× 0 0 0 0 0 1 1 0 = 6 (base 10)
0 0 0 0 0 0 0 0
0 0 1 0 1 0 0 1
0 0 1 0 1 0 0 1
0 0 1 1 1 1 0 1 1 0 = 246 (base 10)
58
59
Binary Division
60
Logical Operations on Binary Numbers
61
Logical Operations with One or Two Bits
AND: Compares 2 bits and if they are both 1, then the result
is 1, otherwise, the result is 0.
OR: Compares 2 bits and if either or both bits are 1, then the
result is 1, otherwise, the result is 0.
XOR: (Exclusive OR) : Compares 2 bits and if exactly one
of them is 1 then the result is 1 otherwise (if the bits are
the same), the result is 0.
NOT: Changes the value of a single bit. If it is a 1, the result
is 0; if it is a 0, the result is 1.
Truth Table of Logical Operations
62
Logical Operations
with One or Two Integers
• NOT
• AND
• OR
• XOR
63
NOT operation
Unary operation which performs logical negation on
each bit, forming the ones' complement of the given
binary value. Digits which were 0 become 1, and vice
versa.
Example:
NOT 0111
= 1000
64
AND operation
• An AND operation takes two binary representations of
equal length and performs the logical AND operation on
each pair of corresponding bits. In each pair, perform
AND operation on two bits
• Example:
0101
AND 0011
= 0001
65
OR operation
• An OR operation takes two bit patterns of equal
length, and produces another one of the same length
by matching up corresponding bits (the first of each;
the second of each; and so on) and performing the
logical OR operation on each pair of corresponding
bits.
• For example:
0101
OR 0011
= 0111
66
XOR Operation
• An exclusive or operation takes two bit
patterns of equal length and performs the
logical XOR operation on each pair of
corresponding bits.
• For example:
0101
XOR 0011
= 0110
67
Symbol Representation
Basic Principles
ASCII Code Table
Unicode Code Table
68
Basic Principles
• Text documents can be decomposed into individual
characters
• It's important to handle character data
• Character data isn't just alphabetic characters, but also
numeric characters, punctuation, spaces, etc
• They need to be represented in binary
• There aren't mathematical properties for character data, so
assigning binary codes for characters is somewhat arbitrary
69
Character Set
• A list of characters and the codes used to represent
each one.
• By using a particular character set, computer
manufacturers have made the processing of data
easier.
• Two popular character sets: ASCII & Unicode
70
ASCII Code Table
• ASCII -- American Standard Code for Information Interchange
- permitted machines from different manufacturers to exchange
data.
• The ASCII standard was developed in 1963
71
The Standard ASCII Code Table
72
The extended ASCII characters
73
Limitations of ASCII character set
• String data types allocated one byte per character
• The extended version of ASCII which have 256
characters is ehough for English but not enough for
international use
• Logographic languages such as Chinese, Japanese,
and Korean need far more than 256 characters for
reasonable representation.
• Vietnamese need 61 characters for representation.
• Where can we find number for our characters?
74
Unicode Code Table
75
Unicode Character Set
76
Some Symbols in the Unicode Character Set
77
Advantages of using Unicode
78
Exercise 3
• Calculate the multiplication, AND, OR, and XOR
operations of the following integers
• 0011 1011 and 0000 1010
• 1101 1000 and 0000 1110
• Calculate the division of
• 1001 0011 and 1011
• 1110 0111 and 1101
79
0011 1011 and 0000 1010
• AND: 00001010
• OR: 0011 1011
• XOR: 0011 0001
• Multiplication: 10 0100 1110
80
1101 1000 and 0000 1110
• AND: 0000 1000
• OR: 1101 1110
• XOR: 1101 0110
• Multiplication: 1011 1101 0000
81
Division
• 1001 0011 and 1011
82
• 1110 0111 1101
83