Integer Representation(Signed and Un-signed Numbers)
- Computers use a fixed number of bits to represent an integer.
- The commonly-used bit-lengths for integers are 8-bit, 16-bit, 32-bit or 64-bit.
- Besides bit-lengths, there are two representation schemes for integers:
- Unsigned Integers: can represent zero and positive integers.
- Signed Integers: can represent zero, positive and negative integers.
n-bit Unsigned Integers
- Unsigned integers can represent zero and positive integers, but not negative integers.
- Example 1: Suppose that n=8 and the binary pattern is 0100 0001B, the value of this unsigned integer is 1×2^0 + 1×2^6 = 65D.
- Example 2: Suppose that n=16 and the binary pattern is 0001 0000 0000 1000B, the value of this unsigned integer is 1×2^3 + 1×2^12 = 4104D.
Signed Integers
Signed integers can represent zero, positive integers, as well as negative integers. Three representation schemes are available for signed integers:
- Sign-Magnitude representation
- 1's Complement representation
- 2's Complement representation
- In all the above three schemes, the most-significant bit (msb) is called the sign bit.
- The sign bit is used to represent the sign of the integer - with 0 for positive integers and 1 for negative integers.
Sign-Magnitude representation
- The most-significant bit (MSB) is the sign bit, with value of 0 representing positive integer and 1 representing negative integer.
- The remaining n-1 bits represents the magnitude (absolute value) of the integer.
- The absolute value of the integer is interpreted as "the magnitude of the (n-1)-bit binary pattern".
- Example 1: Suppose that n=8 and the binary representation is 0 100 0001B.
- Sign bit is 0 means positive Absolute value is 100 0001B = 65D
- Hence, the integer is +65D
- Example 2: Suppose that n=8 and the binary representation is 1 000 0001B.
- Sign bit is 1 means negative Absolute value is 000 0001B = 1D
- Hence, the integer is -1D
n-bit Sign Integers in 1's Complement Representation
- Again, the most significant bit (MSB) is the sign bit, with value of 0 representing positive integers and 1 representing negative integers.
- The remaining n-1 bits represents the magnitude of the integer, as follows:
- for positive integers, the absolute value of the integer is equal to "the magnitude of the (n-1)-bit binary pattern".
- for negative integers, the absolute value of the integer is equal to "the magnitude of the complement (inverse) of the (n-1)-bit binary pattern" (hence called 1's complement).
- Example 1: Suppose that n=8 and the binary representation 0 100 0001B.
- Sign bit is 0 means positive Absolute value is 100 0001B = 65D
- Hence, the integer is +65D
- Example 2: Suppose that n=8 and the binary representation 1 000 0001B.
- Sign bit is 1 means negative Absolute value is the complement of 000 0001B,
- i.e., 111 1110B = 126D Hence, the integer is -126D
n-bit Sign Integers in 2's Complement Representation
- Again, the most significant bit (msb) is the sign bit, with value of 0 representing positive integers and 1 representing negative integers.
- The remaining n-1 bits represents the magnitude of the integer, as follows:
- for positive integers, the absolute value of the integer is equal to "the magnitude of the (n-1)-bit binary pattern".
- for negative integers, the absolute value of the integer is equal to "the magnitude of the complement of the (n-1)-bit binary pattern plus one" (hence called 2's complement).
- Example 1: Suppose that n=8 and the binary representation 0 100 0001B.
- Sign bit is 0 means positive Absolute value is 100 0001B = 65D
- Hence, the integer is +65D
- Example 2: Suppose that n=8 and the binary representation 1 000 0001B.
- Sign bit is 1 means negative
- Absolute value is the complement of 000 0001B plus 1, i.e., 111 1110B + 1B = 127D
- Hence, the integer is -127D
Comments
Post a Comment