Representation of Data within the Computer - HPCCSS

Download Report

Transcript Representation of Data within the Computer - HPCCSS

Representation of Data within the
Computer
Oct., 1999(Revised 2001 Oct)
1
The representation of numeric
data

Denary(十進制) system
265=2 x 102+6 x 101 + 5 x 1

Binary(二進制) system
1100=1 x 23 + 1 x 22 + 0 x 21 + 0 x 20
which is equal to 12 in the denary system.
1100 = 1210
2
Octal and Hexadecimal system

Octal system
2658 = 2 x 82 + 6 x 81 + 5 x 80 = 18110

Hexadecimal system
2B516 = 2 x 162 + 11 x 161 + 5 x 160 = 69310
3
Numeric system Conversion
A. From other systems to denary numbers
 1100112 = 1 x 25 + 1 x 24 + 0 x 23 + 0 x 22 +
1 x 21 + 1 x 20 = 5110
 3528 = 3 x 82 + 5 x 81 + 2 x 80 = 23410
 3E16 = 3 x 161 + 14x 160= 6210
4
Numeric system Conversion
B. From the denary system to other systems
25410 = 3 x 82 + 7 x 81 + 6 x 80 = 3768
In general, to convert a denary number to other
number system with base b, we may use the
following algorithm:
1 Divide the denary number by b repeatedly until the
quotient is 0.
2 Copy the remainders obtained in reverse order to
get the final answer.
5
Exercise




Convert 200010 into a hexadecimal number.
Answer is 7D0
Convert 5110 into a binary number.
Answer is 1010001
6
From binary to octal system
Group the digits of the binary number by
three starting form the right.
2 Replace each group of three digits by an
equivalent octal digit.
Convert 1100112 into octal number.
1
7
From binary to and hexadecimal
system
Group the digits of the binary number by
four starting form the right.
2 Replace each group of four digits by an
equivalent octal digit.
Convert 101101012 into a hexadecimal
number.
1
8
From octal and hexadecimal
systems to binary
1
2
First convert each digit of the octal and
hexadecimal number to a group of three and
four binary binary digits respectively.
Then form the binary number with these
groups of binary digits according to the
sequence of digits of the original number.
9
Example




Convert 3678 into a binary number.
Answer is 11110111
Convert 5C16 into a binary number.
Answer is 101110000010110
10
Fixed point representation
There are three widely used systems for
representing both positive and negative
numbers.
! Sign-and-magnitude
! 1’s-complement
! 2’s -complement
11
Sign-and-magnitude
1
2
the leftmost bit (refer to sign bit) is 0 for
positive numbers and 1 for negative
numbers.
The remaining bits represent the binary
equivalent of the magnitude (absolute value)
of the number.
12
Sign-and-magnitude
Convert +4210 and -1810 to 8-bit binary codes
using the sign-and-magnitude representation.
13
Example
In the sign-and-magnitude system, what is the
number represented by the following 8-bit
binary codes?
1
00000000
2
10000000
3
11010111
14
Two’s complement representation
—
1
2
To obtain the 2’s complement of binary
code, the following algorithm may be useful.
Obtain the 1’s complement by
complementing each bit. That is, change all
0s to 1s to 0s and 1s to 0s.
Add 1 to the result binary code.
15
Example
Find the 2’s complement of the binary code
01001011.
16
Two’s complement representation
—
1
2
The following is an algorithm for
representing a denary number in the 2’s
complement system.
Convert the denary number to equivalent
binary number.
If the denary number is positive, do nothing.
Otherwise, obtain the 2’s complement of the
binary code.
17
Example
Represent the number +42 and -18 in 8-bit 2’s
complement system.
18
Two’s complement representation
By reversing the above algorithm, we can convert
the 2’s complement representation to denary
number. The following is the reversed algorithm.
1 If the leftmost bit is 0, the number is positive. The
binary code is the binary equivalent of the number.
Convert it to a denary number.
2 If the leftmost bit is 1, the number is negative.
Obtain its 2’s complement before converting it to a
denary number. The required number is the
negative value of the denary number obtained. 19
Example
What are the denary numbers represented by
the following representation in 2’s
complement system?
1
01010010
2
10100111
20
Two’s complement representation
The 2’s complement system is much more
common than other systems because of the
following:
1 Subtraction can be done by addition.
Therefore, no additional circuit is needed.
2 A sign bit and other bits need not be
handled separately.
3 There is no distinct +0 and -0
representations as in sign-and-magnitude 21
and 1’s complement systems.
Overflow
Arithmetic operation may not obtain the correct
answer in some cases. This happens when the
result is out of range that the binary codes can
represent.
— For example, when 8-bit binary code is used to
represent a number in the 2’s complement system,
the range of numbers that can be represented is
from -128 to +127. If the result of an operation
lies outside this range, overflow occurs.
—
22
Floating-point representation
The fixed point representation is not
sufficient for scientific calculations, hence,
there is a need to easily accommodate both
very large integers and very small fractions.
— In this case, the position of the binary point
is variable and the binary point is said to
float.
—
23
Normalization of binary numbers
0.0001112 = .1112 x 2-3 = .1112 x 10-11
-1011002 = - .10112 x 26 = - .10112 x 10110
24
Representation
Suppose 16-bit binary codes is used to
represent floating point number. Let us use
the leftmost bit to indicate the sign of the
number. The next 8 bits to represent the
mantissa and the rightmost 7 bits to code
the exponent in 2’s complement.
25
Example
Represent -10.37510
-1010.0112 = -.10100112 x 24
Therefore, the floating representation of 10.375 is
1101001100000100
26
Truncation error
In the truncation scheme, any significant
bit that cannot be accommodated in the
bits for mantissa is ignored. For
example, 101100011 is represented
by .10110001. 0.000000001 is ignored.
This is called a truncation error.
27
Rounding error
In the rounding scheme, if the bits that cannot be
accommodated correspond to a value less than
half of the place value of the last bit used to
represent mantissa, they are ignored, Otherwise,
the place value of the last bit used to represent
mantissa is added to the mantissa.
For example, the former example 101100011 is
represented by .10110010.
28
Representation of alphanumeric data
and Chinese characters
All letters, digits and symbols in a computer are
referred to as alphanumeric characters.
— Usually, 8-bit binary is used to code one character.
It leads to 28 = 256 different characters that can be
represented.
— The most commonly used coding table follows the
standard American Standard Code for
Information Interchange, usually referred by its
short form ASCII.
—
29
Representation of Chinese characters
Chinese characters are not made up of alphabet.
— They need more bits to represent Chinese
characters. Indeed, we use 2 bytes, that is 16 bits
to represent one single Chinese character.
— The most commonly used codes for traditional
Chinese characters is the Big5 code and that for
simplified Chinese characters is GB codes.
—
30
Parity checking奇偶檢驗
Parity checking is a simple method of checking
the correctness of received data.
— An ASCII code is 8 bits long, but only the
rightmost 7 bits are used to represent a character
leaving the most significant bit 0. The 8th bit can
be used as a parity bit.
—
31
Parity checking奇偶檢驗
To maintain even parity 偶數奇偶檢驗, the parity
bit is set to 1 if the code being sent has an odd
number of 1s.
— On the other hand, to maintain odd parity奇數奇
偶檢驗, the parity bit is set to 1 if the code being
sent has an even number of 1s.
—
32