If computers represent non-negative integers (unsigned) only, the binary representation is straightforward, as we had seen earlier. Computers have also to handle negative integers (signed). The normal convention that is followed to distinguish between a signed and unsigned number is to treat the most significant (leftmost) bit in the binary sequence as a sign bit. If the leftmost bit is 0, the number is positive, and if the leftmost bit is 1, the number is negative. 2.10.1 Sign+magnitude representation The simplest form of representing a negative integer is the sign+magnitude representation. In a sequence of n bits, the leftmost bit is used for sign and the remaining n-1 bits are used to hold the magnitude of the integer. Thus in a sequence of 4 bits, 0100 = +4 1100 = -4 As there are several drawbacks in this representation, this method has not been adopted to represent signed integers. There are two representations for 0 in this approach. 0000 = + 010 1000 = – 010

28

Hence it is difficult to test for 0, which is an operation, performed frequently in computers. Another drawback is that, the addition and subtraction require a

consideration of both the sign of the numbers and their relative magnitude, in order to carry out the required operation. This would actually complicate the hardware design of the arithmetic unit of the computer. The most efficient way of representing a signed integer is a 2’s-complement representation. In 2’s complement method, there is only one representation of 0. 2.10.2. 2’s-complement representation This method does not change the sign of the number by simply changing a single bit (MSB) in its representation. The 2’s-complement method used with -ve

numbers only is as follows: a. Invert all the bits in the binary sequence (ie., change every 0 to1 and

every 1 to 0 ie.,1’s complement) b. Add 1 to the result This method works well only when the number of bits used by the system is known in the representation of the number. Care should be taken to pad (fill with zeros) the original value out to the full representation width before applying this algorithm. Example: In a computer that uses 8-bit representation to store a number, the wrong and right approaches to represent –23 are as follows: Wrong approach: The binary equivalent of 23 is 10111. Invert all the bits => 01000 Add 1 to the result => 01001 Pad with zeros to make 8-bit pattern => 00001001 => +9 Right approach: The binary equivalent of 23 is 10111 Pad with zeros to make 8-bit pattern => 00010111 Invert all the bits => 11101000 Add 1 to the result => 11101001 => -23 2.10.3 Manual method to represent signed integers in 2’s complement form This is an easier approach to represent signed integers. This is for -ve numbers only.

29

Step 1: Copy the bits from right to left, through and including the first 1. Step 2: Copy the inverse of the remaining bits. Example 1: To represent –4 in a 4-bit representation: The binary equivalent of the integer 4 is 0100 As per step1, copy the bits from right to left, through and including the first 1 => 100 As per step2, copy the inverse of the remaining bits => 1 100 => -4 Example 2: To represent –23 in a 8-bit representation: The binary equivalent of 23 is 00010111 As per step 1: 1 As per step 2: 11101001 => -23 2.10.4 Interpretation of unsigned and signed integers Signed number versus unsigned number is a matter of interpretation. A single binary sequence can represent two different values. For example, consider a binary sequence 111001102. The decimal equivalent of the above sequence when considered as an unsigned integer is: 111001102 = 23010 The decimal equivalent of the sequence when considered as a signed integer in 2’s complement form is: 111001102 = -2610 (after 2’s complement and add negative sign). When comparing two binary numbers for finding which number is greater, the comparison depends on whether the numbers are considered as signed or unsigned numbers. Example: X = 1001 Y = 0011 Is ( X > Y) /* Is this true or false? */ It depends on whether X and Y are considered as signed or unsigned.

30

If X and Y are unsigned: X is greater than Y If X and Y are signed: X is less than Y. 2.10.5 Range of unsigned and signed integers In a 4-bit system, the range of unsigned integers is from 0 to 15, that is, 000 to 1111 in binary form. Each bit can have one of two values 0 or 1. Therefore, the total number of patterns of 4 bits will be 2 X 2 X 2 X 2 = 16. In an n-bit system, the total number of patterns will be 2n.. Hence, if n bits are used to represent an unsigned integer value, the range is from 0 to 2n-1, that is, there are 2n different values. In case of a signed integer, the most significant (left most) bit is used to represent a sign. Hence, half of the 2n patterns are used for positive values and the other half for negative values. The range of positive values is from 0 to 2n-1-1 and the range of negative values is from –1 to –2n-1. In a 4-bit system, the range of signed integers is from –8 to +7.

## No Comments