# Representing negative integers in binary trading

Of all possible encodings of negative numbers, two have been used most often: Signed magnitude representations simply designate the MSB as the sign bit, and the remaining bits as magnitude. In an 8-bit signed-magnitude system, '16' would be represented as '', and '' as ''.

This system is easy for humans to interpret, but it has a major disadvantage for digital circuits: Because of this, a simple operation like '2—3', which requires counting backwards from two three times, will not yield the expected result of '-1', but rather the largest negative number in the system. A better system would place the smallest positive and negative numbers immediately adjacent to one another in the count range, and this is precisely what the 2's compliment representation does.

The number wheels below illustrate signed-magnitude and 2's compliment encodings for 8-bit numbers. In 2's compliment encoding, the MSB still functions as a sign bit—it is always '1' for a negative number, and '0' for a positive number.

The 2's compliment code has a single '0' value, defined by a bit pattern containing all 0's including the leading '0'. In other words, we can represent one more non-zero negative number than positive, and the magnitude of the largest negative number is one greater than the magnitude of the largest positive number. The disadvantage to 2's compliment encoding is that negative numbers are not easily interpreted by humans e.

A simple algorithm exists for converting a positive number to a 2's compliment-encoded negative number of the same magnitude, and for converting a 2's compliment- encoded negative number to a positive number of the same magnitude. The algorithm, illustrated in Fig. 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. Like sign-and-magnitude representation, ones' complement has two representations of 0: 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: In the previous example, the first binary addition gives , which is incorrect.

The correct result only appears when the carry is added back in. A remark on terminology: Note that the ones' complement representation of a negative number can be obtained from the sign-magnitude representation merely by bitwise complementing the magnitude.

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 in an unsigned sense than the ones' complement of the positive value.

In two's-complement, there is only one zero, represented as Negating a number whether negative or positive is done by inverting all the bits and then adding one to that result.

Addition of a pair of two's-complement integers is the same as addition of a pair of unsigned numbers except for detection of overflow , if that is done ; the same is true for subtraction and even for N lowest significant bits of a product value of multiplication.

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. Biased representations are now primarily used for the exponent of floating-point numbers. The IEEE floating-point standard defines the exponent field of a single-precision bit number as an 8-bit excess field.

The double-precision bit exponent field is an bit excess field; see exponent bias. It also had use for binary-coded decimal numbers as excess In conventional binary number systems, the base, or radix , is 2; thus the rightmost bit represents 2 0 , the next bit represents 2 1 , the next bit 2 2 , and so on.

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.

The following table shows the positive and negative integers that can be represented using four bits. Same table, as viewed from "given these binary bits, what is the number as interpreted by the representation system":.

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 unsigned 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 , John Colson advocated reducing expressions to "small numbers", numerals 1, 2, 3, 4, and 5.