Transcript Chapter 1

Chapter 4
Integer Data
Representation
Unsigned Integers
Range of Values




1
2
4
8
Byte:
Bytes:
Bytes:
Bytes:
0-255
0-65,535
4,294,967,295
264 - 1
Implementation


Generally, built-in machine
instructions exist for
manipulating and calculating
single-precision (32 bit) or
double precision (64 bit)
integers
If not, software procedures may
exist to handle multiple storage
locations for integers
Signed Integers
Signed-Magnitude
 1’s Complement
 2’s Complement
NOTE:
Only latter is typically
implemented due to serious
limitations of others.

Signed-Magnitude



Choose a single bit to represent
the sign - usually most significant
bit
0 => + and 1=>negative
Maximum absolute value is 231-1
(assuming 32 bit representation)
 2,147,483,647
Problems


In arithmetic operations, must
test signs of both operands to
determine process
Two zeroes exist: 0
Thus, comparison of 0s
becomes an issue
Complements Representation
The sign of the number is a natural
result of the arithmetic operations,
and therefore the sign does not have
to be handled separately.
Calculations are consistent for all
different signed combinations of the
operands.
1’s Complement


Numbers whose binary
representation begins with a 0
are considered positive and
those that begin with a 1 are
negative
To get the negative
representation of a positive
number, one takes the
complement of the number
1’s Complement
8-bit representation
Observations





Range of number values is split in the
middle
Even numbers begin with 0 and positive
numbers with 1
To add numbers, regardless of sign, one
just does a regular addition (wraparound)
Suppose a negative and positive number
are added. End-around carry may result.
Overflow can occur
Basis for 1’s Complement
Subtracting a value from some standard base value is known as
taking the complement. In binary representation for 8-bits, say, we
would subtract a positive number from 11 111 111 in order to
derive its negative representation.
Example -58:
11 111 111
- 00 111 010
========
11 000 101
NOTE: The negative is just the inversion of the positive number.
1’s Complement Addition
58
+ 22
==
80
00 111 010
00 010 110
========
01 010 000
106
- 2
===
1
01 101 010
11 111 101
========
01 100 111
1 end-around
======== carry
01 101 000
64
+ 65
==
129
01 000 000
01 000 001
104
========
10 000 001 => overflow
=> result exceeded range of values
1s Complement
Testing for Overflow
If both operands have the same sign and the sign of the result is
different, then overflow has occurred.
NOTE:
Some early interpreted versions of Pascal limited word
size to 16 bits. When the interpreter was subsequently executed
on 32-bit machines, overflow was not detected.
1’s Complement Problems
• Addition may require end-around
carry.
• Two zeroes exist: 0
Thus, comparison of 0’s becomes
an issue
2’s Complement
Representation
Basis for 2’s Complement
The modulus in 2’s complement is a 1 followed by a string of 0’s.
In binary representation for 8-bits, say, we would subtract a
positive number from 100 000 000 in order to derive its negative
representation.
Example -58:
100 000 000
- 00 111 010
========
11 000 110
NOTE: The negative representation is just the inversion of the positive
number plus 1.
2’s Complement
Addition
58
+ 22
==
80
00 111 010
00 010 110
========
01 010 000
106 01 101 010
- 2
11 111 110
=== ========
104 101 101 000
64
+ 65
==
129
01 000 000
01 000 001
========
10 000 001 => overflow
=> result exceeded range of values
ignore
carry bit
2s Complement
Testing for Overflow
An overflow has occurred if when the result overflows into the
sign bit. Thus overflow can be detected if the sign of the result is
opposite that of both operands.
A carry-out bit is ignored.