Unit 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. Representation and operations of floating numbers
2.6. Logical operations on Binary Numbers
2.7. Symbol Representation
2
2.1. Number Systems
• Decimal System
• Base-b System
• Conversion from Decimal to Base-b
• Binary System
• Hexadecimal System
• Octal System
3
Number Categories
• Natural numbers
• Integers
• Rational numbers
• Irrational numbers
• Numbers are written using positional notation
943 = 9 x 102 + 4 x 10 + 3
Only in base 10 (decimal)
4
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
where n is the total number of digits, and dj is
the j th digit from the right most position in the
decimal number.
5
Rational/Irrational numbers
• dn-1dn-2 . . . . d1d0.d-1d-2…..d-m
𝑛 −1
∑ 𝑑 𝑗 ∗1 0 𝑗
𝑗 =−𝑚
• 8943043.456
6
Decimal System
• The system has ten as its base
• Uses various symbols (called digits) for no more than
ten distinct values (0, 1, 2, 3, 4, 5, 6, 7, 8 and 9) to
represent any number
• Decimal separator indicates the start of a fractional part
• Sign symbols + (positive) or − (negative) in front of the
numbers to indicate sign
7
Base-b System
• b digits is used in the representation of numbers.
• Example
1410 = 226 = 1123 = 11102
• If i is an integer written in base - b with digits d j:
i = dn-1dn-2 . . . . d1d0
then i represents the sum: Convert from base b
n 1
to base 10
j
d *
j 0
b j
where n is the total number of digits, and d j 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
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=32510
9
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
10
Example 1
Use Remainder Method to convert 12 (base 10) to base 2
11
More examples
• 63 = 1111112
• 20 = 101002
• 81 = 10100012
• 126 = 11111102
12
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.
• Disregard the whole number part of the previous result
and multiply by b once again. The whole number part of
this new result is the second digit to the right of the point.
• Continue this process until you get a zero as number’s
decimal part or until you recognize an infinite repeating
pattern.
13
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,
The representation of .625(10)= .101(2)
14
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
15
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
16
•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)
17
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 2 k
symbols
18
Example
Convert 11101.112 to Decimal
19
Hexadecimal System
Hexadecimal numbers have 16 different digits, that are represented
by the numbers from 0 to 9 and the letters A, B, C, D, E and F.
Example
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
• 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
28
Representing Audio Information
• Digitize the sound wave
• Sampling: periodically measure the voltage of the
signal and record appropriate value
• Reproduce the sound: Use stored voltage values to
create a new continuous electronic signal
• Audio format : WAV,AU, AIFF, VQF, MP3
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 (2 10)
rather than the 1000 (103)
35
Units of information
36
Representation of Integers
• Unsigned Integers
• Signed Integers
37
Unsigned Integers
• Unsigned integers are represented by a fixed number of bits (typically 8,
16, 32, and/or 64)
• Only a finite set of numbers that can be represented:
• With 8 bits, 0…255 (0016…FF16) can be represented;
• With 16 bits, 0…65535 (000016…FFFF16) can be represented
• If an operation on bytes has a result outside this range, it will cause an
‘overflow’
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 = 00101010 Convert to binary
11010101 Complement the bits
11010101 Add 1 to the complement
+ 00000001
11010110 Result is -42 in two's complement
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
11010110 Two's complement of 42
Throw away the
high-order 00110010 Add 50 and -42
carry as we are + 11010110
using a eight bit 100001000
representation
00001000 Is the eight-bit result
43
• 42 = 00101010
• -42 = 11010110
44
Two's Complement Arithmetic
• A signed binary integer number :
an-1an-2...a2a1a0
• The value of A will be represented as:n 2
A an 1 2 n 1
ai 2 i
i 0
• Range: [-2n-1, 2n-1-1]
10000…000
……….
01111…111
• Negative – positive numbers?
45
With n = 8 bit
• Range is [-128, 127] • Round-circle numbers in
0000 0000 = 0 computers
0000 0001 = +1
0000 0010 = +2
.......
01111111 = +127
10000000 = -128
10000001 = -127
.......
1111 1110 = -2
1111 1111 = -1
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
• What are the values of the following signed 8-bit integers in decimal
system?
• 0011 1101 = 32 + 16 +8 +4 +1 = 61
• 1000 1001 = -128 + 8 + 1 = -119
• 1111 1100 = -128 + 64 +32 +16+4 = -4
• 0001 1010 = 16+8+2=26
47
Operations on Integers
• Addition and Subtraction
• Multiplication and Division
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)
00010011 + 00111110 = 01010001
1 1 1 1 1 carries
0 0 0 1 0 0 1 1 = 19 (base 10)
+0 0 1 1 1 1 1 0 = 62 (base 10)
0 1 0 1 0 0 0 1 = 81 (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
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)
00010111 × 00000011 = 01000101
0 0 0 1 0 1 1 1 = 23 (base 10)
× 0 0 0 0 0 0 1 1 = 3 (base 10)
1 1 1 1 1 carries
0 0 0 1 0 1 1 1
0 0 0 1 0 1 1 1
0 0 1 0 0 0 1 0 1 = 69 (base 10)
58
59
Binary Division
Binary division follows the same rules as in
decimal division .
60
Real number representation
(floating point number)
• To present real numbers in computers, we use
Floating Point Number
• Example: 12.3 = 12.3 * 100
= 123 * 10-1
= 1.23 * 101
61
Real integer representation (floating point number)
• A real number X could be represented as floating point
number:
X = M * RE
Where:
• M is Mantissa
• R is Radix, typicaly 2 or 10.
• E is Exponent
• With a fixed R, we only need M and E as integers to
store/ represent M
62
Example – represent floating
number
• With R = 10, assume that there are 2 floating
numbers N1 and N2 stored as mantissa and
exponent parameters:
• M1 = -15 và E1 = +12
• M2 = +314 và E2 = -9
• Meaning
N1 = M1 x 10 E1 = -15x1012
= -15 000 000 000 000
and
N2 = M2 x 10 E2 = 314 x 10-9
= 0.000 000 314
63
b. Athrimetic operations with
floating numbers
• We can perform athrimetic operations by using
mantissa and exponent.
64
c. Athrimetic operations with
floating numbers (2)
• Assume that there are two floating numbers as
followed:
• N1 = M1 x RE1 và N2 = M2 x RE2
• Then, we can perform athrimetic operations as
followed:
• N1 ± N2 = (M1 x R E1-E2 ± M2) x RE2 ,
(giả thiết E1 ≥ E2)
• N1 x N2 = (M1x M2) x R E1+E2
• N1 /N2 = (M1 / M2) x R E1-E2
65
c. IEEE 754/85 standard
• The standard for floating numbers
• Radix R = 2
• There are 3 formats :
• Single precision, 32-bit
• Double precision, 64-bit
• Double extended precision, 80-bit
66
c. IEEE 754/85 standard (2)
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
67
c. IEEE 754/85 standard (3)
• S is the signed bit, S=0 means positive, S=1 means
negative.
• e is the biased exponent of the real exponent E, tức
là: E = e – b
Where b is the bias :
• 32-bit : b = 127, or E = e - 127
• 64-bit : b = 1023, or E = e - 1023
• 80-bit : b = 16383, or E = e - 16383
68
c. IEEE 754/85 standard (4)
• m is the normalized mantissa of the mantissa M
or M = 1.m
• The fomular to determine the value of the
floating number is :
X = (-1)S x 1.m x 2e-b
S e m
69
Example 1
• Example 1: A floating number X is represented as
IEEE 754-32 standard as followed:
1100 0001 0101 0110 0000 0000 0000 0000
Calculate the value of X in the decimal system.
• Solution:
• S = 1 X is a negative number
• e = 1000 0010 = 130
• m = 10101100...00
• So X = (-1)1 x 1.10101100...00 x 2130-127
= -1.101011 x 23 = -1101.011 = -13.375
70
Example 2
• Calculate the value of X in the decimal system if the
representation of X in IEEE 754-32 standard as
followed:
0011 1111 1000 0000 0000 0000 0000 0000
• Solution:
• S = 0 X is a positive number
• e = 0111 1111= 127
• m = 000000...00
• Vậy X = (-1)0 x 1.0000...00 x 2127-127
= 1.0 x 20 = 1
71
Example 3
• Represent X = 9.6875 as a floating number using IEEE
754 standard (32-bit)
• Solution:
X = 9.6875(10) = 1001.1011(2) = 1.0011011 x 23
We have:
• S = 0 because this is a positive number
• E = e – 127 e = 127 + 3 = 130(10) = 1000 0010(2)
• m = 001101100...00 (23 bit)
X = 0100 0001 0001 1011 0000 0000 0000 0000
72
Range of representation
underflow
overflow overflow
-¥ -b -a -0 +0 a b +¥
• 32 bit: a = 2-127 ≈ 10-38 b = 2+127 ≈ 10+38
• 64 bit: a = 2-1023 ≈ 10-308 b = 2+1023 ≈ 10+308
• 80 bit: a = 2-16383 ≈ 10-4932 b = 2+16383 ≈ 10+4932
73
Some special representation
• All bits of e are 0 and all bits of m are 0 X = 0
• All bits of e are 1 and all bits of m are 0 X =
• All bits of e are 1 and at least one bit of m is 1, X
is not a number (NaN)
74
Exercises
1. Represent the following numbers in IEEE 754
standard (32 bit)
a. X = 0.75
b. Y = -27.0625
2. What are the decimal value of the following IEEE-754
numbers
a. X = 1100 0000 1110 0100 0000 0000 0000 0000
b. Y = 0100 0100 0001 1000 0100 0000 0000 0000
75
Logical Operations on Binary Numbers
• Logical Operations with One or Two Bits
• Logical Operations with One or Two Integers
76
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 isTruth
a 0, the result is 1.
Table of Logical Operations
77
Logical Operations
with One or Two Integers
• NOT
• AND
• OR
• XOR
78
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
79
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
80
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
81
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
82
Logical shift
• Shift Left Logical
• Shift Right Logical
83
Exercise
• 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
84
0011 1011 and 0000 1010
• AND: 00001010
• OR: 0011 1011
• XOR: 0011 0001
• Multiplication: 10 0100 1110
85
1101 1000 and 0000 1110
• AND: 0000 1000
• OR: 1101 1110
• XOR: 1101 0110
• Multiplication: 1011 1101 0000
86
Symbol Representation
Basic Principles
ASCII Code Table
Unicode Code Table
87
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
88
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
89
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
• ASCII standard originally used 7 bits to represent characters ->
consists of 128 binary values (0 to 127), each associated with a
character or command
• The extended ASCII character set used 8 bits to represent
characters. It also consists 128 characters representing
additional special, mathematical, graphic and foreign
characters.
90
The Standard ASCII Code Table
91
The extended ASCII characters
92
Chương 1: Thông tin và biểu diễn thông tin
2. Biểu diễn dữ liệu trong máy tính 2.5 Biểu diễn ký tự
93
Limitations of ASCII character set
• String data types allocated one byte per character
• The extended version of ASCII which have 256
characters is enough 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?
2bytes per character?
94
Unicode Code Table
Before Unicode was invented. . . .
• There were hundreds of different encoding systems
• No single encoding could contain enough characters
• Encoding systems conflict with one another : two
encodings can use the same number for two different
characters, or use different numbers for the same
character.
95
Unicode Character Set
• Provides a unique number for every character
• Uses 16 bits or more per character
• Has been adopted by such industry leaders as HP, IBM,
Microsoft, Oracle, Sun, and many others.
• Is supported in many operating systems, all modern
browsers, and many other products.
96
Some Symbols in the Unicode Character
Set
97
Advantages of using Unicode
• Significant cost savings
• Enables software products to be targeted across multiple
platforms, languages and countries without re-engineering.
• Allows data to be transported through many different
systems without corruption.
98