Sunday 21 October 2012

Chapter 2 Signed Integer Representation

Signed Number Representation

Signed magnitude


MSB






LSB

                                                   Magnitude

A signed-magnitude number has a sign as its left-most bit (also referred to as the high-order bit or the most significant bit) while the remaining bits represent the magnitude (or absolute value) of the numeric value. 

Example:  -39
The MSB integer is represented positive and negative by 0 and 1, and the rest of the integers are magnitude.
In binary, 39 is 0 010 0111.
Therefore the signed magnitude for -39 is 1 010 0111.

NOTE: 
Addition arithmetic calculations on integers that are represented using this notation
(1) If the signs are the same, add the magnitudes and use that same sign for the result;
(2) If the signs differ, you must determine which operand has the larger magnitude. The sign of the result is the same as the sign of the operand with the larger magnitude, and the magnitude must be obtained by subtracting (not adding) the smaller one from the larger one.

Examples:
01001100+00100100

         11       <<<< carries
0  1001100     (76)
0+0100100   +(36)
========   ====
0  1110000   (112)

For this example, there is no overflow because it do not have 7th "carry in" count from the left.



01001100+01100101

1<==11     <<<< carries
0  1001100   (  76)
0+1100101 +(101)
========  ====
0  0110001   (  49)

From the answer, we see an overflow cause the binary calculation obtained the erroneous result 76+101=49.


NOTE:
Signed-magnitude subtraction is carried out in a manner similar to decimal arithmetic, where it is sometimes necessary to borrow from digits in the minuend.

Examples:
01000100 - 00110001

    012  012 << borrows
0  1000100     (68)
0 -0110001    -(49)
========  ====
0  0010011     (19)

We find 01000100 - 00110001 = 00010011 in signed-magnitude representation.

00100100 - 01100100

1   2     << borrows
0   0100100    (36)
0  -1100100   -(90)
========    ====
1   1000000   (-64)

We need to change the sign of the difference, because the subtrahend value are bigger than minuend value. We find that 00100100(36) - 01100100(90) = 11000000(-64) in signed-magnitude representation.

If there are 2 difference sign, then we need to determine which of the value is larger in magnitude. It sign will be the sign of the result.

Example:
10010010 and 00110100

            02 << borrows
0    0110100    (52)
1   -0010010   -(18)
=========  ====
0    0100010    (34)


The final sign of the result will be positive(0) because the value of positive is larger in magnitude. We find that the result is positive sign [0]0100010(34).


One's Complement
In the one's complement representation, all positive integers are represented in their correct binary format. For example "+25" is represented as usual by 00011001. However, its complement "-25" is obtained by invert 0 and 1.
For the example, the one's complement representation of "-25" will be 11100110.

Another example:

+5 ===>>>   00000101
 -5 ===>>>   11111010
NOTE: The 0 and 1 of the most significant bit(MSB) represented positive and negative sign.

Below are some test for one's complement:
The example for sum of minus 9 and plus 11,

+11==> 00001011
  -9==>  11110110
==============
(1)          00000001 << Sum of minus 9 and plus 11.
From the example, we find that the sum of minus 9 and plus 11 by using one's complement is "00000001" that is incorrect and. However, the result of sum should be "00000010", "plus 2" or the result "1" plus the carry of (1).
It did not work. However the one's complement representation does not always gain the correct result. So we will still use another representation which is called two's complement that involved with one's complement.


Two's Complement

In two’s complement, positive integers are represented in standard binary, as in signed magnitude. However, the representation of a negative number is determined as follows: 
(1) compute a binary representation of the magnitude of the number(singed-magnitude), 
(2) flip all the bits(one's complement), and 
(3) add 1. 
For example,
the 8-bit two’s complement representation of 7 is 00000111 (as before) while −7 is represented as follows (using the steps given above):

The positive integer:   0000 0111   ( 7 )
Reflect each bit     :   1111 1000
Add one                :   1111 1001   ( minus 7 )

From the example, we can see that two's complement is evolved from the one's complement.

Example:
An addition between positive 9 and negative 7.

11    111     1   <<< carries
  0    000 1001     (9)
  1  +111 1001     (-7)
===========    ===
  0    000 0010      (2)

The result is 0000 0010, this is a correct answer. However, the 8th "carry into" bit count from the right to left will be discard and this do not result from an overflow condition.

NOTE: When the carry into or out of the most significant bit must be carefully as this may result from an overflow condition.
Overflow will occur in four situations:
1.adding large positive number
2.adding large negative number
3.subtracting a large positive number from a large negative number
4.subtracting a large negative number from a large positive number





No comments:

Post a Comment