Sunday, May 10, 2015

Signed vs. Unsigned Integers in C

2's complement Encoding

The most common binary encoding technique for integers is called 2's complement. A signed integer is represented with “N” bits will have a range from -2N-1 to 2N-1 – 1.

The encoding algorithm is as follows.

1. The high-order (leftmost) bit of the word is the sign bit. If the sign bit is 1, the number is negative; otherwise, the number is positive.

2. Positive numbers follow the normal binary sequence.

    0 = 000...0002
    1 = 000...0012
          ...

3. In an N-bit word, omitting the sign bit, there are “N-1” bits for the positive integers which can represent the integers from 0 to 2N-1 – 1.

4. To negate an integer, complement all the bits in the word and then add 1 to the result. For example, to form negative integer -9, start with 9 = 000..10012, complement all the bits to get   111..01102, then add 1 to get -9 = 111..01112 = 247.

5. The maximum negative value, -2N-1, does not have a positive equivalent; negating this value produces the same value.

1's complement Encoding

Another encoding scheme which is used for representing integers is 1's complement. In this scheme, each positive number is represented by normal binary encoding. To obtain a negative number, each bit is simply complemented. For example, to form negative integer -9, start with 9 = 000..10012 and complement each bit to get -9 = 111..01102.

The range of numbers that can be represented for a N-bit word is -(2N-1 – 1) to 2N-1 – 1. 1's complement represents 1 less negative value and has both “-0” and “0”.

Sign Magnitude Encoding

The sign magnitude encoding is similar to the 1's complement encoding. To negate an integer, it just complements the sign bit. For example, to form negative integer -9, start with 9 = 000..10012 and complement the sign bit to get -9 = 100..10012.

The range of numbers that can be represented for a N-bit word is -(2N-1 – 1) to 2N-1 – 1. 1's complement represents 1 less negative value and has both “-0” and “0”.

Signed vs. Unsigned

Unsigned integers are always stored using straight binary notation regardless of whether the signed types use 2-s complement, 1-s complement or sign magnitude notation. For example, in 2-s complement notation, the bit pattern 111...111 (n bits long) can represent either “-1” (using the signed notation) or 2N – 1 (using the unsigned notation). The integers from “0” to 2N-1 – 1 are represented identically in both signed and unsigned notations.

Whether an integer is signed or unsigned affects the operations performed on it. All arithmetic operations on unsigned integers behave according to the rules of modular (congruence) arithmetic modulo 2N. For example, adding 1 to the largest value of an unsigned type is guaranteed to produce “0”. The behaviour of overflow is well-defined.

Expressions that mix signed and unsigned integers are often forced to use unsigned operations. When a negative signed integer is promoted to an unsigned integer of the same or larger type, it is first promoted to the signed equivalent of the larger type, then converted to the unsigned value.

The conversion rules are as follows.



No comments:

Post a Comment