Signed number representations


In computing, signed number representations are required to encode negative numbers in binary number systems.
In mathematics, negative numbers in any base are represented by prefixing them with a minus sign. However, in computer hardware, numbers are represented only as sequences of bits, without extra symbols. The four best-known methods of extending the binary numeral system to represent signed numbers are: sign-and-magnitude, [|ones' complement], [|two's complement], and [|offset binary]. Some of the alternative methods use implicit instead of explicit signs, such as negative binary, using the [|base −2]. Corresponding methods can be devised for other bases, whether positive, negative, fractional, or other elaborations on such themes.
There is no definitive criterion by which any of the representations is universally superior. For integers, the representation used in most current computing devices is two's complement, although the Unisys ClearPath Dorado series mainframes use ones' complement.

History

The early days of digital computing were marked by a lot of competing ideas about both hardware technology and mathematics technology. One of the great debates was the format of negative numbers, with some of the era's most expert people having very strong and different opinions. One camp supported two's complement, the system that is dominant today. Another camp supported ones' complement, where any positive value is made into its negative equivalent by inverting all of the bits in a word. A third group supported "sign & magnitude", where a value is changed from positive to negative simply by toggling the word's sign bit.
There were arguments for and against each of the systems. Sign & magnitude allowed for easier tracing of memory dumps as small numeric values use fewer 1 bits. Internally, these systems did ones' complement math so numbers would have to be converted to ones' complement values when they were transmitted from a register to the math unit and then converted back to sign-magnitude when the result was transmitted back to the register. The electronics required more gates than the other systemsa key concern when the cost and packaging of discrete transistors were critical. IBM was one of the early supporters of sign-magnitude, with their 704, 709 and 709x series computers being perhaps the best-known systems to use it.
Ones' complement allowed for somewhat simpler hardware designs as there was no need to convert values when passed to and from the math unit. But it also shared an undesirable characteristic with sign-magnitudethe ability to represent negative zero. Negative zero behaves exactly like positive zero; when used as an operand in any calculation, the result will be the same whether an operand is positive or negative zero. The disadvantage, however, is that the existence of two forms of the same value necessitates two rather than a single comparison when checking for equality with zero. Ones' complement subtraction can also result in an end-around borrow. It can be argued that this makes the addition/subtraction logic more complicated or that it makes it simpler as a subtraction requires simply inverting the bits of the second operand as it is passed to the adder. The PDP-1, CDC 160 series, CDC 3000 series, CDC 6000 series, UNIVAC 1100 series, and the LINC computer use ones' complement representation.
Two's complement is the easiest to implement in hardware, which may be the ultimate reason for its widespread popularity. Processors on the early mainframes often consisted of thousands of transistorseliminating a significant number of transistors was a significant cost savings. Mainframes such as the IBM System/360, the GE-600 series, and the PDP-6 and PDP-10 use two's complement, as did minicomputers such as the PDP-5 and PDP-8 and the PDP-11 and VAX. The architects of the early integrated circuit-based CPUs chose to use two's complement math. As IC technology advanced, virtually all adopted two's complement technology. x86, m68k, Power ISA, MIPS, SPARC, ARM, Itanium, PA-RISC, and DEC Alpha processors are all two's complement.

Signed magnitude representation

This representation is also called "sign–magnitude" or "sign and magnitude" representation. In this approach, a number's sign is represented with a sign bit: setting that bit to 0 for a positive number or positive zero, and setting it to 1 for a negative number or negative zero. The remaining bits in the number indicate the magnitude. For example, in an eight-bit byte, only seven bits represent the magnitude, which can range from 0000000 to 1111111. Thus numbers ranging from −12710 to +12710 can be represented once the sign bit is added. For example, −4310 encoded in an eight-bit byte is 10101011 while 4310 is 00101011. Using signed magnitude representation has multiple consequences which makes them more intricate to implement:
  1. There are two ways to represent zero, 00000000 and 10000000.
  2. Addition and subtraction require different behaviour depending on the sign bit, whereas one's complement can ignore the sign bit and just do an end-around carry, and two's complement can ignore the sign bit and depend on the overflow behavior.
  3. Comparison also require inspecting the sign bit, whereas in two's complement, one can simply subtract the two numbers, and check if the outcome is positive or negative.
  4. The minimum negative number is −127 instead of −128 in the case of two's complement.
This approach is directly comparable to the common way of showing a sign. Some early binary computers use this representation, perhaps because of its natural relation to common usage. Signed magnitude is the most common way of representing the significand in floating-point values.

Ones' complement

Alternatively, a system known as ones' complement can be used to represent negative numbers. The ones' complement form of a negative binary number is the bitwise NOT applied to it, i.e. the "complement" of its positive counterpart. Like sign-and-magnitude representation, ones' complement has two representations of 0: 00000000 and 11111111.
As an example, the ones' complement form of 00101011 becomes 11010100. The range of signed numbers using ones' complement is represented by to and ±0. A conventional eight-bit byte is −12710 to +12710 with zero being either 00000000 or 11111111.
To add two numbers represented in this system, one does a conventional binary addition, but it is then necessary to do an end-around carry: that is, add any resulting carry back into the resulting sum. To see why this is necessary, consider the following example showing the case of the addition of −1 to +2 :

binary decimal
11111110 −1
+ 00000010 +2
─────────── ──
1 00000000 0 ← Not the correct answer
1 +1 ← Add carry
─────────── ──
00000001 1 ← Correct answer

In the previous example, the first binary addition gives 00000000, which is incorrect. The correct result only appears when the carry is added back in.
A remark on terminology: The system is referred to as "ones' complement" because the negation of a positive value x can also be formed by subtracting x from the ones' complement representation of zero that is a long sequence of ones. Two's complement arithmetic, on the other hand, forms the negation of x by subtracting x from a single large power of two that is congruent to +0. Therefore, ones' complement and two's complement representations of the same negative value will differ by one.
Note that the ones' complement representation of a negative number can be obtained from the sign-magnitude representation merely by bitwise complementing the magnitude.

Two's complement

The problems of multiple representations of 0 and the need for the end-around carry are circumvented by a system called two's complement. In two's complement, negative numbers are represented by the bit pattern which is one greater than the ones' complement of the positive value.
In two's-complement, there is only one zero, represented as 00000000. Negating a number is done by inverting all the bits and then adding one to that result. This actually reflects the ring structure on all integers modulo 2N:. Addition of a pair of two's-complement integers is the same as addition of a pair of unsigned numbers ; the same is true for subtraction and even for N lowest significant bits of a product. For instance, a two's-complement addition of 127 and −128 gives the same binary bit pattern as an unsigned addition of 127 and 128, as can be seen from the 8-bit two's complement table.
An easier method to get the negation of a number in two's complement is as follows:
Example 1Example 2
1. Starting from the right, find the first "1"0010100100101100
2. Invert all of the bits to the left of that "1"1101011111010100

Method two:
  1. Invert all the bits through the number
  2. Add one
Example: for +2, which is 00000010 in binary :
  1. ~00000010 → 11111101
  2. 11111101 + 1 → 11111110

    Offset binary

, also called excess-K or biased representation, uses a pre-specified number K as a biasing value. A value is represented by the unsigned number which is K greater than the intended value. Thus 0 is represented by K, and −K is represented by the all-zeros bit pattern. This can be seen as a slight modification and generalization of the aforementioned two's-complement, which is virtually the representation with negated most significant bit.
Biased representations are now primarily used for the exponent of floating-point numbers. The IEEE 754 floating-point standard defines the exponent field of a single-precision number as an 8-bit excess-127 field. The double-precision exponent field is an 11-bit excess-1023 field; see exponent bias. It also had use for binary-coded decimal numbers as excess-3.

Base −2

In conventional binary number systems, the base, or radix, is 2; thus the rightmost bit represents 20, the next bit represents 21, the next bit 22, and so on. However, a binary number system with base −2 is also possible.
The rightmost bit represents, the next bit represents, the next bit and so on, with alternating sign. The numbers that can be represented with four bits are shown in the comparison table below.
The range of numbers that can be represented is asymmetric. If the word has an even number of bits, the magnitude of the largest negative number that can be represented is twice as large as the largest positive number that can be represented, and vice versa if the word has an odd number of bits.

Comparison table

The following table shows the positive and negative integers that can be represented using four bits.
DecimalUnsignedSign and magnitudeOnes' complementTwo's complementExcess-8 Base −2
+16
+15 1111
+14 1110
+13 1101
+12 1100
+11 1011
+10 1010
+9 1001
+8 1000
+7 01110111011101111111
+6 01100110011001101110
+5 010101010101010111010101
+4 010001000100010011000100
+3 001100110011001110110111
+2 001000100010001010100110
+1 000100010001000110010001
+0 000000000000000010000000
−0 000010001111000010000000
−1 10011110111101110011
−2 10101101111001100010
−3 10111100110101011101
−4 11001011110001001100
−5 11011010101100111111
−6 11101001101000101110
−7 11111000100100011001
−8 100000001000
−9 1011
−10 1010
−11

Same table, as viewed from "given these binary bits, what is the number as interpreted by the representation system":
BinaryUnsignedSign and magnitudeOnes' complementTwo's complementExcess-8Base −2
00000000−80
00011111−71
00102222−6−2
00113333−5−1
01004444−44
01015555−35
01106666−22
01117777−13
10008−0−7−80−8
10019−1−6−71−7
101010−2−5−62−10
101111−3−4−53−9
110012−4−3−44−4
110113−5−2−35−3
111014−6−1−26−6
111115−7−0−17−5

Other systems

Google's Protocol Buffers "zig-zag encoding" is a system similar to sign-and-magnitude, but uses the least significant bit to represent the sign and has a single representation of zero. This allows a variable-length quantity encoding intended for nonnegative integers to be used efficiently for signed integers.
Another approach is to give each digit a sign, yielding the signed-digit representation. For instance, in 1726, John Colson advocated reducing expressions to "small numbers", numerals 1, 2, 3, 4, and 5. In 1840, Augustin Cauchy also expressed preference for such modified decimal numbers to reduce errors in computation.