Chapter 4 Integer Data Representation
Unsigned Integers
Range of Values l 1 Byte: l 2 Bytes:0-65,535 l 4 Bytes:4,294,967,295 l 8 Bytes:
Implementation l Generally, built-in machine instructions exist for manipulating and calculating single-precision (32 bit) or double precision (64 bit) integers l If not, software procedures may exist to handle multiple storage locations for integers
Signed Integers l Signed-Magnitude l 1’s Complement l 2’s Complement NOTE: Only latter is typically implemented due to serious limitations of others.
Signed-Magnitude l Choose a single bit to represent the sign - usually most significant bit l 0 => + and 1=>negative l Maximum absolute value is (assuming 32 bit representation) 2,147,483,647
Problems l In arithmetic operations, must test signs of both operands to determine process l 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 l Numbers whose binary representation begins with a 0 are considered positive and those that begin with a 1 are negative l To get the negative representation of a positive number, one takes the complement of the number
1’s Complement 8-bit representation
Observations l Range of number values is split in the middle l Even numbers begin with 0 and positive numbers with 1 l To add numbers, regardless of sign, one just does a regular addition (wraparound) l Suppose a negative and positive number are added. End-around carry may result. l 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 in order to derive its negative representation. Example -58: ======== NOTE: The negative is just the inversion of the positive number.
1’s Complement Addition ===================== end-around ======== carry == ======== => 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 in order to derive its negative representation. Example -58: ======== NOTE: The negative representation is just the inversion of the positive number plus 1.
2’s Complement Addition ===================== ignore carry bit == ======== => overflow => result exceeded range of values
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.