Upload
kristopher-jenkins
View
236
Download
0
Tags:
Embed Size (px)
Citation preview
Chapter 4
Integer Data Representation
Unsigned Integers
Range of Values
1 Byte: 0-255 2 Bytes: 0-65,535 4 Bytes: 4,294,967,295 8 Bytes: 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 ComplementNOTE:
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: 0Thus, 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 Complement8-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 00 111 010 106 01 101 010 + 22 00 010 110 - 2 11 111 101
== ======== === ========80 01 010 000 1 01 100 111
1 end-around64 01 000 000 ======== carry
+ 65 01 000 001 104 01 101 000== ========
129 10 000 001 => overflow => result exceeded range of values
1s ComplementTesting for Overflow
If both operands have the same sign and the sign of the result isdifferent, 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: 0Thus, comparison of 0’s becomes an issue
2’s ComplementRepresentation
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 ComplementAddition
58 00 111 010 106 01 101 010 + 22 00 010 110 - 2 11 111 110
== ======== === ========80 01 010 000 104 101 101 000 ignore
carry bit64 01 000 000
+ 65 01 000 001== ========
129 10 000 001 => overflow => result exceeded range of values
2s ComplementTesting 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.