 Conversion between different B Characters Representation Signed Numbers Representations Computers use binary format to represent all types of information. To represent signed numbers there are three options:

## Signed Magnitude Representation

A sign bit is used to indicate the sign of the number, e.g. a 0 indicates a + sign and a 1 indicates a - sign (negative number). The rest of the number is just the magnitude in binary format.

 Examples on sign-magnitude representation of signed numbers +9 is represented as 01001 -9 is represented as 11001

Range: Using n bits, the range of numbers that can be represented is from –(2n-1 - 1) to +2n-1 - 1 .

 Examples: Ranges of numbers using sign-magnitude notation Examples: Using 8 bits, we can represent numbers in the range: -127 to +127 Using 12 bits, we can represent numbers in the range: -2047 to +2047

Advantages: Simple (number representation is straight forward).

Disadvantages:

1. Has the problem of double representing the 0 (–0 and +0),
2. Complicates the design of the logic circuits that handle signed-numbers arithmetic,
3. This is because each of the sign and magnitude parts has to be processed separately,
4. Also, the sign of both numbers have to be examined before the actual operation (addition or subtraction) is determined,
5. Separate circuits are required to do the addition and subtraction operations.

## 1's Complement Representation

Positive numbers are represented using normal binary equivalent while negative numbers are represented by the 1's complement (complement) of the normal binary representation of the magnitude.

 Examples on signed numbers representation using 1`s complement +9 is represented as 01001 -9 is represented as 10110 (obtained by complementing the binary representation of 9). Important Note: Negative numbers will always have a 1 in the MSB while positive numbers will have a 0 in the MSB.

Range: Using n bits, the range of numbers that can be represented is from –(2n-1 - 1) to +2n-1 - 1 .

Advantages:

1. Still relatively simple to represent the numbers,
2. Simpler Add/Subtract circuit design (subtracting a number from another involves complementing the subtracted and then adding it to the other number).

Disadvantages:
1. Has the problem of double representing the 0 (0…000 and 11…111),
2. The Add/Subtract operations are still relatively complex,
3. This is because the last carry bit has to be examined to determine if an overflow has occurred, a further addition of a 1 is necessary (i.e. Addition/Subtraction is done in two-steps) or the output should be complemented.
Overflow can occur when adding two numbers with the same sign (i.e. both positive or both negative) and the result is outside the range of possible numbers. Overflow can be detected by checking if the sign bit of the result is different from that of both operands. If the two operands have different signs, then overflow cannot occur and no need to check it.

 Examples illustrating 1`s complement Add/Subtract operations Example 1: Adding 0111 + 0111 The result is 1110 and the carry out is 0, hence an overflow has occurred (adding two positive numbers resulted in a negative number, -1 in this case). Example 2: Subtracting 0001 - 0111 First the subtrahend is complemented and becomes 1000. Then it is added to the minuend and the result is 1001 with no end carry. The result represent –6 (the correct result) and no further addition is required. Example 3: Subtracting 0111 - 0001 Again, the subtrahend is complemented and becomes 1110. Then it is added to the minuend and the result is 0101 with an end carry of 1. This carry has to be added to the previous result and yields 0110 (+6, the correct answer).

## 2's Complement Representation

Positive numbers are represented using normal binary equivalent while negative numbers are represented by the 2's complement (complement) of the normal binary representation of the magnitude. The 2's complement of a binary number equals its 1's complement + 1.

 Examples of 2`s complement representation of signed numbers The easiest way to obtain the 2's complement of a binary number is by starting at the LSB, leaving all the 0s unchanged, look for the first occurrence of a 1. Leave this 1 unchanged and complement all the bits after it. +9 is represented as 01001 To represent –9, first obtain the binary equivalent: 01001. Now leave the LSB 1 unchanged and complement all the remaining bits to get 10111, which represent –9 in 2's complement. Important Note: As with 1's complement, negative numbers will always have a 1 in the MSB while positive numbers will have a 0 in the MSB.

Range: Using n bits, the range of numbers that can be represented is from –2n-1 to +2n-1 - 1 . The -2n-1 is represented by the 1000…000 code, which has no 2's complement, hence the unsymmetrical range. E.g. if 8 bits are used, the range of numbers that can be represented is from –128 to +127.

Advantages:

1. No double representation of 0 (the 2's complement of 0 is still 0),
2. Simplest Add/Subtract circuit design (subtracting a number from another involves 2's complementing the subtracted and then adding it to the other number),
3. Add/Subtract operations is done in one-step, the end carry is only examined to determine if an overflow has occurred (as shown in the example below), otherwise it is discarded.
4. The end result is already represented in 2's complement (only if there is no overflow).

That is why this is the most preferred method for signed-number representations in computers.

 Examples illustrating 2`s complement Add/Subtract operations Example 1: Adding 0111 + 0111 The result is 1110 (-2), hence an overflow has occurred (adding two positive numbers resulted in a negative number). Overflow detection in 2's complement Add/Subtract operations: If the last two carries are not equal, an overflow has occurred In example 1 above, the last carry out was 0 while the carry-before the last was 1, hence an overflow can be detected. Example 2: Subtracting 0001 - 0111 First the subtrahend is 2's complemented and becomes 1001. Then it is added to the minuend and the result is 1010 with the last two carries being 0 (no overflow). The result represents –6 in 2's complement (the correct result) and no further operation is required. Example 3: Subtracting 0111 - 0001 Again, the subtrahend is complemented and becomes 1111. Then it is added to the minuend and the result is 0110 (+6, the correct result). Since the last two carries being 1 no overflow has occurred and no further operations are required.

Disadvantages:
1. The unsymmetrical range, which is not a serious problem,
2. It is slightly more complex to obtain the 2's complement (it involves complementing and adding 1). However this can be accomplished very easily during the Add/Subtract operations (by making the first carry in 1 and complementing the subtrahend).

## Sign Extension

In many situations we need to move a signed-number from one location to another with larger number of bits. This has to be done while keeping both magnitude and sign correct. This procedure is called Sign-Extension . For numbers represented in 1's or 2's complement the procedure is described below:

 Sign-Extension Procedure for signed-numbers represented in 1`s and 2`s complement notations To move an n-bit signed number into an m-bit location with m>n: Put the n-bit number in the lowest-significance n-bits of the m-bit location, Fill all the remaining higher significance (m-n bits) with the sign bit (i.e. the nth bit). This ensures that both the magnitude and sign of the number are preserved. Examples on sign-extension: Represent the number 1101 (-3 in 2's complement or –2 in 1's complement) using 8-bits: Answer: Applying the procedure above we get: 11111101, which is –00000011 (i.e. –3) in 2's complement and –00000010 (i.e. –2) in 1's complement. Hence both the magnitude and sign were preserved. Represent the number 0101 (+5 in both1's and 2's complement) using 8-bits: Answer: Applying the procedure above we get: 00000101, which is still +5 in both 1's and 2's complement.