04_IntegerRepresentation

Download Report

Transcript 04_IntegerRepresentation

Integer Representation for Computers
Computer Organization and Assembly Language: Module 4
Representation of Integers
 Unsigned
Integer Representation
 Sign Magnitude
 Complement Representations
 Biased Representations
 Sign Extension
Unsigned Integer Representation
 The
representation of a non-negative integer using
binary, weighted positional notation is called
unsigned integer representation
 Given n bits, it is possible to represent the range of
values from 0 to 2n - 1
 For
example an 8-bit representation would allow
representations that range 0 to 255
Unsigned Integer Representation
three bits, we can
represent 8 distinct
values
 For unsigned integers,
these values are 0-7
 We don’t use modulo
arithmetic
0
0
0
 With

Results greater than 7 or
less than 0 are overflow
1
1
1
1
1
0
0
0
1
0
1
7
6
2
5
1
0
1
3
4
1
0
0
0
1
1
0
1
0
Sign Magnitude
 An
extra bit in the most significant
position is designated as the sign bit
which is to the left of an unsigned
integer. The unsigned integer is the
magnitude.
 A 0 in the sign bit means positive,
and a 1 means negative.
x
Sign
Magnitude
xxx xxxx
Sign Magnitude
an n-bit sign magnitude number the
range of values that it can represent is
-(2n-1-1) to +(2n-1-1)
 Given
 Sign
magnitude representation associates a
sign bit with a magnitude that represents
zero, thus it has two distinct representation
of zero:
00000000 and 10000000
sign bit
Sign Magnitude
With three bits, we can
represent 8 distinct
values
 But since –0 = +0, we
waste one of these
 Having 2 distinct zeroes
makes designing
arithmetic operations
more difficult
0
0
0

1
1
1
1
1
0
0
0
1
0
1
-3
-2
2
-1
1
0
1
3
0
1
0
0
0
1
1
0
1
0
Complement Representation
 Positive
integers have the same representation
as unsigned integers
 Negative integers have a one in the high order
bit, and represented by a bit-wise complement
of their absolute value.
One’s Complement
 The
additive inverse of a number in one’s
complement representation is found by
inverting each bit.
 Inverting each bit is also called “taking the
one’s complement.”
One’s Complement
With three bits, we can
represent 8 distinct
values
 For ones complement,
these are –3 to 3
 Like sign-magnitude,
there are 2 zeroes
0
0
0

1
1
1
1
1
0
0
0
1
0
1
0
-1
2
-2
1
0
1
3
-3
1
0
0
0
1
1
0
1
0
Example of One’s Complement
0000 0011 (3)
1111 1100 (-3)
1110 1000 (-23)
0001 0111 (23)
0000 0000 (0)
1111 1111 (0)
Note: There are two
representations of zero
Two’s Complement
 The
additive inverse of a two’s complement
integer can be obtained by adding 1 to its one’s
complement (overflow ignored)
 This operation is self-reversible: the 2’s
complement of the 2’s complement of N = N.
 An advantage of two’s complement is that there is
only one representation for zero
 0000
 invert  1111  add 1  (1) 0000
Two’s Complement
With three bits, we can
represent 8 distinct
values
 For two’s complement,
these are –4 to 3
 There is a unique zero;
all eight representation
are used for distinct
numbers
0
0
0

1
1
1
1
1
0
0
0
1
0
1
-1
-2
2
-3
1
0
1
3
-4
1
0
0
0
1
1
0
1
0
6 bit 2’s complement example
010001 (17)
101110
+
1
——————
101111 (-17)
1101000 (-24)
0010111
+
1
———————
0011000 (24)
Two’s Complement
 In
two’s complement, one more negative value
than positive value is represented
 The
most negative number has no additive inverse
within a fixed precision.
 Note
that computing the additive inverse is a
mathematical operation. Taking the complement
is an operation on the representation.
Biased Representation
the unsigned representation includes integers from 0 to
M, then subtracting approximately M/2 from the unsigned
interpretation would shift the range from
-(M/2) to +(M/2)
 If
 If
a sequence of bits has a value N when interpreted as an
unsigned integer, it has a value n - bias interpreted as a
biased-n number.
 Usually, the bias is either 2n or 2n-1 for an (n+1)-bit
representation
Biased-4 3-bit Representation
three bits, we can
represent 8 distinct values
 For a bias of –22, or –4, the
range of values is –4 to 3
 There is a unique zero; all
eight representation are
used for distinct numbers
0
0
0
 With
1
1
1
1
1
0
0
0
1
-4
3
-3
1
-1
0
-2 1
0
2
1
0
1
0
1
0
0
0
1
1
Biased-127 8-bit Example
Given 0000 01102, what is it’s value in a biased-127
representation. Assume an 8-bit representation.
The value of the unsigned integer:
0000 01102 = 610
Its value in biased-127 is:
6 - 127 = -121
Sign Extension
 For
integer representation, the size are commonly
8, 16, 32 and 64.
 It is occasionally necessary to convert an integer
representation from one size to another
 from
8 bits to 32 bits
 from 16 bits to 32 bits
 We
must maintain the same value while changing
the size of the representation
Sign Extension - Unsigned
 Place
the original integer into the least significant
portion and stuff the remaining positions with 0’s.
xxxxxxxx
00000000xxxxxxxx
8 bits
16 bits
Sign Extension - Signed
 The
sign bit of the smaller representation is
placed into the sign bit of the larger
representation
 The magnitude is put into the least significant
portion and all remaining positions are stuffed
with 0’s.
sxxxxxxx
s00000000xxxxxxx
Sign Extension - complement
positive number, a 0 is used to pad the
remaining positions.
0xxxxxxx
 For
000000000xxxxxxx
negative number, a 1 is used to pad the
remaining positions.
 For
1xxxxxxx
111111111xxxxxxx