Data Representation

advertisement
Data Representation
 We shall deal with two forms of data, numerical data and
non-numerical data.
 These two types of data appear to be represented, in the
computer, in very similar fashions (everything is 0’s and
1’s)
 The context in which the data is used determines how
the data is treated.
 An example of non-numerical (unweighted code) is the
opcode and ASCII codes.
 Numerical data (weighted code) includes numbers both
signed and unsigned.
 Weighted code means that the various components of the
code have values, or weights, associated with their
position within the code. In non-numerical or
unweighted code, each individual component of the code
has no more significance than any other part; it is simply
the overall pattern of the code that is important. Think of
your social security number.
Number Systems
 Each number system is identified by a base (we call the
radix) e.g., the decimal system is base 10
 These systems are called weighted positional notation.
The integer expressed in these systems is a weighted
sum of its digits.
 Each system allows a number of symbols, this number is
equal the radix of the system. E.g., the decimal system
allows 10 symbols 0 - 9
 To express a number greater than the largest symbol, we
use a combination of these symbols.
 For a sequence of n digits, s0, s1, s2, ....,sn-1, if sn-1  0, the
summation
N = s0 . b0 + s1 . b1 + s2 . b2 + ..... +sn-1 . bn-1
ex.
38510 = 3 . 102 + 8 . 101 + 5 . 100
 The symbol carrying the most weight, sn-1 is designated
the most significant digit, while the symbol carrying the
least weight, s0 is designated the least significant digit.
 Computers use the binary system (base 2) which uses
two symbols, 0 and 1.
 We will use other systems such as the Octal (base 8)
which uses symbols 0 -7, and the Hexadecimal system
(base 16) which uses symbols 0- 9 , A -F
 Digital circuitry provides an inexpensive way to design
computers and it easily discriminates between two
values. Thus the use of the binary system.
 With n digits in system base r we can represent up to rn
different integers. Ex. with 4 digits in decimal system we
can represent 1000 ( 0 - 9999)
Transformations between bases
 Converting from decimal to any other base is done in
two ways:
1- Least significant digit first
2- Most significant digit first
 The LSDF method determines the digit in the new base
least significant digit first. It employs repetitive division.
The number is divided by the radix of the new base.
ex.
converting from decimal to binary
8010
division
80/2
40/2
20/2
10/2
5/2
2/2
1/2
quotient
40
20
10
5
2
1
0 stop
remainder
0
LSD
0
0
0
1
0
1
MSD
the binary number is 1010000
ex.
Converting decimal to hex
94610
division
946/16
59/16
3/16
quotient
59
3
0 stop
remainder
2
11 (B)
3
the hex number is 3B216
 In the Most significant digit first, conversion is done
using repeated subtractions. Finds the most significant
digit first.
ex. convert 8010 to binary
80 - 26(=64) = 16
16 - 24(=16) = 0
8010 = 1.26 + 0.25 + 1.24 + 0.23 + 0.22 + 0.21 + 0.20
= 1010000
 Converting between bases that are both powers (ex. Hex,
Octal, and binary) of the same number is done by
grouping.
 To convert from binary to any base power of 2 (base 2x)
for some x is simple: group the binary digits together in
groups of x, starting from LSD. Add leading zeros if
necessary to obtain a whole number of groups.
1110110010
is grouped
001 110 110
this is 16628
010 (in base 8)
and is grouped
0011 1011 0010 (in hex base 16)
this is 3B216
Binary
0000
0001
0010
0011
0100
0101
0110
0111
1000
Hex
0
1
2
3
4
5
6
7
8
Binary
1001
1010
1011
1100
1101
1110
1111
Hex
9
A
B
C
D
E
F
 So to convert from any base to a power of two base, it is
easier to convert first to binary then convert the binary
into the new base.
 To convert from any base power of two to binary:
convert each digit in the base into x bits equivalent to
that digit.
ex.
45FE716
0100 0101
1111
1110
0111
Signed and Unsigned Numbers
 The computer represents everything using binary format
(0’s and 1’s). Characters, for example, are represented
using the ASCII code.
 Numbers, both signed (positive and negative) and
unsigned (natural numbers) are represented in binary,
weighted positional notation.
 An n bit unsigned integer representation can represent
the range of values from 0 to 2n - 1
An n bit sequence
bn-1bn-2......b2b1b0
represents the integer
N = b0 . 20 + b1 . 21 + b2 . 22 + ..... +bn-1 . 2n-1
 So in an unsigned representation every bit contributes to
the value of the integer
Signed Integers
 For signed numbers we need a way to represent the sign
of the number. On paper, this is easy. We use “-” or “+”.
 We make the leftmost bit (MSB) be a sign bit
1
0
for negative
for positive
 This way we lose a bit (the one we use for the sign), and
therefore the range of values is dropped to half.
 With N bits we can represent numbers from
- 2n-1 to 2n-1 -1
ex.
with 4 bits we car represent values from -8 to 7. If we need
to represent values outside this range...we will need more
bits.
Negative Numbers Representation
 There are three ways to represent negative numbers
1. Sign-Magnitude
2. One’s Complement
3. Two’s Complement
1- Sign-Magnitude
Use the same representation as positive number, but
with 1 for the sign bit.
sign
magnitude
ex.
using 8 bits
5
-5
0 0000101
1 0000101
Problems with this method:operations on these numbers are hard (addition or
subtraction)
Complement Representation
 There are two kinds of complements for each number
system. The r’s and (r-1)’s complement
ex.
decimal
binary
hex
10’s and 9’s complement
2’s and 1’s complement
16’s and 15’s complement
 (r-1)’s complement and r’s complement are used to
represent negative numbers. Most computer architectures
use the two’s complement to represent negative
numbers.
 The r’s complement is obtained from the r-1 complement
and adding a one.
(r-1)’s Complement
 Subtract each digit of the number from r-1 (the radix of
the system - 1)
ex.
95210
999
9520 4 7 (9’s complement )
 To represent a negative number using the one’s
complement, write out the value (absolute value) of the
number. then subtract each digit from r-1
ex.
5
0000 0101
to represent -5
11111111
000001011 1 1 1 1 0 1 0 (this is -5 in one’s complement)
 Notice that the sign bit is correct (it became 1) which
indicates a negative number.
 Problems with using r-1’s complement
- zero has two representations
+0
0000 0000
-0
1111 1111
but +0 and -0 are equal (so the machine has to know
this)
- Addition is harder
-3 + 5
+3
-3
0000 0011
1111 1100
1
+5
0000 0101
0000 0101
1111 1100 +
0000 0001
the carry must be added to least significant bit of the result
and carried to the second least significant bit if necessary,
etc.
0000 0001
1+
0000 0010
 In addition, using the r-1 complement, the carry out must
be added to LSB of the result to get the correct value.
r’s Complement
1- get the (r-1)’s complement
2- add 1 to the result
ex.
93510
999
9350 6 4 (r-1)’s complement
064
1+
065
 When using the r’s complement we add normally, and
ignore any carry from the MSD (most significant digit)
 If the result is negative, it will be in r’s complement
form
ex.
395 - 210
210 is 789 + 1 = 790 in 10's complement form
395
790+
185
Why Use 2’s Complement
 The left-most bit is still a sign bit
1 for negative
0 for positive
 One way to write 0
+0
0000 0000
-0
0000 0000 (how?)
 With n bits we can represent -2n-1 to ( 2n-1 - 1)
 Subtraction is done by taking 2’s complement and
adding
 2’s complement of 2’s complement is the original
number
 The 2’s complement of a binary number is the same as
the 16’s complement of corresponding Hex.
ex.
1 3 F B16 - 0 2 1 C16
1 3FB
FDE4+
1 1DF
ex.
789A16 - 001D16
789A
FFE3+
787D
Overflow
A situation occurs because the magnitude of the results of
arithmetic operations have become too large for the fixed
word length of the computer to represent them properly.
(out or range result)
ex.
using 4- bit (signed numbers)
710 - 310
0007
9997+
0004
-710 + 310
9993
0003+
9996
710 + 310
0007
0003+
0010
Download