Signed Number Representations

  • Upload
    manjues

  • View
    13

  • Download
    0

Embed Size (px)

DESCRIPTION

number representation

Citation preview

  • Signed number representations 1

    Signed number representationsIn 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 fourbest-known methods of extending the binary numeral system to represent signed numbers are: sign-and-magnitude,ones' complement, two's complement, and excess-K. Some of the alternative methods use implicit instead of explicitsigns, such as negative binary, using the base 2. Corresponding methods can be devised for other bases, whetherpositive, negative, fractional, or other elaborations on such themes. There is no definitive criterion by which any ofthe representations is universally superior. The representation used in most current computing devices is two'scomplement, although the Unisys ClearPath Dorado series mainframes use ones' complement.

    HistoryThe early days of digital computing were marked by a lot of competing ideas about both hardware technology andmathematics technology (numbering systems). One of the great debates was the format of negative numbers, withsome of the era's most expert people having very strong and different opinionsWikipedia:Citation needed. One campsupported two's complement, the system that is dominant today. Another camp supported ones' complement, whereany positive value is made into its negative equivalent by inverting all of the bits in a word. A third group supported"sign & magnitude" (sign-magnitude), where a value is changed from positive to negative simply by toggling theword's sign (high-order) bit.There were arguments for and against each of the systems.Wikipedia:Citation needed Sign & magnitude allowed foreasier tracing of memory dumps (a common process 40 years ago) as numeric values tended to use fewer 1bitsWikipedia:Citation needed. Internally, these systems did ones' complement math so numbers would have to beconverted to ones' complement values when they were transmitted from a register to the math unit and thenconverted back to sign-magnitude when the result was transmitted back to the registerWikipedia:Citation needed.The electronics required more gates than the other systems a key concern when the cost and packaging of discretetransistors was critical. IBM was one of the early supporters of sign-magnitude, with their 7090 (709x series)computers perhaps the best known architecture to use it.Ones' complement allowed for somewhat simpler hardware designs as there was no need to convert values whenpassed to and from the math unit. But it also shared an undesirable characteristic with sign-magnitude the ability torepresent negative zero (0). Negative zero behaves exactly like positive zero; when used as an operand in anycalculation, 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 checkingfor equality with zero. Ones' complement subtraction can also result in an end-around borrow (described below). Itcan be argued that this makes the addition/subtraction logic more complicated or that it makes it simpler as asubtraction requires simply inverting the bits of the second operand as it is passed to the adder. The PDP-1, CDC 160series, CDC 6000 series, UNIVAC 1100 series, and the LINC computer used ones' complement representation.Two's complement is the easiest to implement in hardware, which may be the ultimate reason for its widespreadpopularityWikipedia:Citation needed. Processors on the early mainframes often consisted of thousands of transistors eliminating a significant number of transistors was a significant cost savings. Mainframes such as the IBMSystem/360, the GE-600 series, and the PDP-6 and PDP-10 used two's complement, as did minicomputers such asthe PDP-5 and PDP-8 and the PDP-11 and VAX. The architects of the early integrated circuit-based CPUs (Intel8080, etc.) chose to use two's complement math. As IC technology advanced, virtually all adopted two's complementtechnology. x86, m68k, Power Architecture, MIPS, SPARC, ARM, Itanium, PA-RISC, and DEC Alpha processorsare all two's complement.

  • Signed number representations 2

    Signed magnitude representation(Also called "sign-magnitude" or "sign and magnitude" representation.) In the first approach, the problem ofrepresenting a number's sign can be to allocate one sign bit to represent the sign: set that bit (often the mostsignificant bit) to 0 for a positive number, and set to 1 for a negative number. The remaining bits in the numberindicate the magnitude (or absolute value). Hence in a byte with only 7 bits (apart from the sign bit), the magnitudecan range from 0000000 (0) to 1111111 (127). Thus you can represent numbers from 12710 to +12710 once you addthe sign bit (the eighth bit). A consequence of this representation is that there are two ways to represent zero,00000000 (0) and 10000000 (0). This way, 4310 encoded in an eight-bit byte is 10101011.This approach is directly comparable to the common way of showing a sign (placing a "+" or "" next to thenumber's magnitude). Some early binary computers (e.g., IBM 7090) used this representation, perhaps because of itsnatural relation to common usage. Signed magnitude is the most common way of representing the significand infloating point values.

    Ones' complementMain article: Ones' complement

    8 bit ones' complement

    Binary value Ones' complement interpretation Unsigned interpretation

    00000000 +0 0

    00000001 1 1

    01111101 125 125

    01111110 126 126

    01111111 127 127

    10000000 127 128

    10000001 126 129

    10000010 125 130

    11111101 2 253

    11111110 1 254

    11111111 0 255

    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 the "complement" of its positivecounterpart. Like sign-and-magnitude representation, ones' complement has two representations of 0: 00000000 (+0)and 11111111 (0).As an example, the ones' complement form of 00101011 (4310) becomes 11010100 (4310). The range of signednumbers using ones' complement is represented by (2N11) to (2N11) and 0. A conventional eight-bit byte is12710 to +12710 with zero being either 00000000 (+0) or 11111111 (0).To add two numbers represented in this system, one does a conventional binary addition, but it is then necessary todo 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 (11111110) to +2 (00000010).

  • Signed number representations 3

    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 binary addition alone gives 00000000, which is incorrect. Only when the carry is addedback in does the correct result (00000001) appear.This numeric representation system was common in older computers; the PDP-1, CDC 160 series, and UNIVAC1100/2200 series, among many others, used ones'-complement arithmetic.A remark on terminology: The system is referred to as "ones' complement" because the negation of a positive valuex (represented as the bitwise NOT of x) can also be formed by subtracting x from the ones' complementrepresentation of zero that is a long sequence of ones (0). Two's complement arithmetic, on the other hand, formsthe negation of x by subtracting x from a single large power of two that is congruent to +0.[1] 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-magnituderepresentation merely by bitwise complementing the magnitude.

    Two's complement

    8 bit two's complement

    Binary value Two's complement interpretation Unsigned interpretation

    00000000 0 0

    00000001 1 1

    01111110 126 126

    01111111 127 127

    10000000 128 128

    10000001 127 129

    10000010 126 130

    11111110 2 254

    11111111 1 255

    Main article: Two's complementThe problems of multiple representations of 0 and the need for the end-around carry are circumvented by a systemcalled two's complement. In two's complement, negative numbers are represented by the bit pattern which is onegreater (in an unsigned sense) than the ones' complement of the positive value.In two's-complement, there is only one zero, represented as 00000000. Negating a number (whether negative or positive) is done by inverting all the bits and then adding 1 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

  • Signed number representations 4

    unsigned numbers (except for detection of overflow, if that is done); the same is true for subtraction and even for Nlowest significant bits of a product (value of multiplication). For instance, a two's-complement addition of 127 and128 gives the same binary bit pattern as an unsigned addition of 127 and 128, as can be seen from the 8bit two'scomplement table.An easier method to get the negation of a number in two's complement is as follows:

    Example 1 Example 2

    1. Starting from the right, find the first '1' 00101001 00101100

    2. Invert all of the bits to the left of that one 11010111 11010100

    Method two:1.1. Invert all the bits through the number2.2. Add oneExample: for +1 which is 00000001 in binary:1. ~00000001 111111102. 11111110 + 1 11111111 (1 in two's complement)

    Excess-K

    8 bit excess-127

    Binary value Excess-127 interpretation Unsigned interpretation

    00000000 127 0

    00000001 126 1

    01111111 0 127

    10000000 1 128

    11111111 +128 255

    Excess-K, also called offset binary or biased representation, uses a pre-specified number K as a biasing value. Avalue 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 theaforementioned two's-complement, which is virtually the excess-(2N1-1) representation with negated mostsignificant bit.Biased representations are now primarily used for the exponent of floating-point numbers. The IEEE floating-pointstandard defines the exponent field of a single-precision (32-bit) number as an 8-bit excess-127 field. Thedouble-precision (64-bit) exponent field is an 11-bit excess-1023 field; see exponent bias. It also had use for binarycoded decimal numbers as excess-3.

  • Signed number representations 5

    Base 2See also: Negative baseIn conventional binary number systems, the base, or radix, is 2; thus the rightmost bit represents 20, the next bitrepresents 21, the next bit 22, and so on. However, a binary number system with base 2 is also possible. Therightmost bit represents (2)0 = +1, the next bit represents (2)1 = 2, the next bit (2)2 = +4 and so on, withalternating 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 magnitudeof the largest negative number that can be represented is twice as large as the largest positive number that can berepresented, and vice versa if the word has an odd number of bits.

    Comparison tableThe following table shows the positive and negative integers that can be represented using 4 bits.

    4 bit integer representations

    Decimal Unsigned Sign and magnitude Ones' complement Two's complement Excess-8 (biased) Base 2

    +16 N/A N/A N/A N/A N/A N/A

    +15 1111 N/A N/A N/A N/A N/A

    +14 1110 N/A N/A N/A N/A N/A

    +13 1101 N/A N/A N/A N/A N/A

    +12 1100 N/A N/A N/A N/A N/A

    +11 1011 N/A N/A N/A N/A N/A

    +10 1010 N/A N/A N/A N/A N/A

    +9 1001 N/A N/A N/A N/A N/A

    +8 1000 N/A N/A N/A N/A N/A

    +7 0111 0111 0111 0111 1111 N/A

    +6 0110 0110 0110 0110 1110 N/A

    +5 0101 0101 0101 0101 1101 0101

    +4 0100 0100 0100 0100 1100 0100

    +3 0011 0011 0011 0011 1011 0111

    +2 0010 0010 0010 0010 1010 0110

    +1 0001 0001 0001 0001 1001 0001

    +0 0000 0000 0000 0000 1000 0000

    0 1000 1111

    1 N/A 1001 1110 1111 0111 0011

    2 N/A 1010 1101 1110 0110 0010

    3 N/A 1011 1100 1101 0101 1101

    4 N/A 1100 1011 1100 0100 1100

    5 N/A 1101 1010 1011 0011 1111

    6 N/A 1110 1001 1010 0010 1110

    7 N/A 1111 1000 1001 0001 1001

  • Signed number representations 6

    8 N/A N/A N/A 1000 0000 1000

    9 N/A N/A N/A N/A N/A 1011

    10 N/A N/A N/A N/A N/A 1010

    11 N/A N/A N/A N/A N/A N/A

    Same table, as viewed from "given these binary bits, what is the number as interpreted by the representation system":

    Binary Unsigned Sign and magnitude Ones' complement Two's complement Excess-8 Base 2

    0000 0 0 0 0 8 0

    0001 1 1 1 1 7 1

    0010 2 2 2 2 6 2

    0011 3 3 3 3 5 1

    0100 4 4 4 4 4 4

    0101 5 5 5 5 3 5

    0110 6 6 6 6 2 2

    0111 7 7 7 7 1 3

    1000 8 0 7 8 0 8

    1001 9 1 6 7 1 7

    1010 10 2 5 6 2 10

    1011 11 3 4 5 3 9

    1100 12 4 3 4 4 4

    1101 13 5 2 3 5 3

    1110 14 6 1 2 6 6

    1111 15 7 0 1 7 5

    Other systemsGoogle's Protocol Buffers "zig-zag encoding" is a system similar to sign-and-magnitude, but uses the leastsignificant bit to represent the sign and has a single representation of zero. This has the advantage to makevariable-length quantity encoding efficient with signed integers.[2]

    Another approach is to give each digit a sign, yielding the signed-digit representation. For instance, in 1726, JohnColson advocated reducing expressions to "small numbers", numerals 1, 2, 3, 4, and 5. In 1840, Augustin Cauchyalso expressed preference for such modified decimal numbers to reduce errors in computation.

    References[1] Donald Knuth: The Art of Computer Programming, Volume 2: Seminumerical Algorithms, chapter 4.1[2] Protocol Buffers: Signed Integers (http:/ / developers. google. com/ protocol-buffers/ docs/ encoding#types)

    Ivan Flores, The Logic of Computer Arithmetic, Prentice-Hall (1963) Israel Koren, Computer Arithmetic Algorithms, A.K. Peters (2002), ISBN 1-56881-160-8

  • Article Sources and Contributors 7

    Article Sources and ContributorsSigned number representations Source: http://en.wikipedia.org/w/index.php?oldid=618981086 Contributors: Abuserjd, Ah adodc, Alksentrs, Anon126, Anthony Appleyard, Antilived, AntonyLu, Atka1989, B105xor, Bookinvestor, Booyabazooka, Burn, Charles Esson, Ckape, Cliff, Damian Yerrick, DavidCary, Delldot, DerHexer, Doug Bell, Dreadstar, Dsimic, Dtgriscom, Dulciana,Dysprosia, EddEdmondson, EnJx, Etu, Fireice, Freudent, Furrykef, GabrieleLaBarba, Giftlite, Goodvac, Gpvos, Greatwalk, Guy Harris, Hairy Dude, Herbee, Holger Blasum, Hon-3s-T,Htmlcoderexe, ITMaverix, Incnis Mrsi, Jfairley, Jkrause79, Jnc, Jonathan de Boyne Pollard, Jonverve, Joost Pjotr Vermeer, JordiGH, Jrme, Karn, Kbolino, Kbrose, Keka, Kev098, KnightRider,Kvng, LancerSix, Leboite, LikeFunYouAre, Loadmaster, Maarten van Emden, Magioladitis, Mekong Bluesman, Michael Hardy, Mindmatrix, Mormegil, Mufka, Muon, NTownsend, Natural Cut,Neoflame, Nitish92, Noe, Novaaesa, Numbo3, O.Koslowski, Oleg Alexandrov, Omicronpersei8, Oxymoron83, Patrick, Peter B., Phatom87, Positivecharge, Proginoskes, Quuxplusone, R'n'B, R.S. Shaw, RTC, Rgdboer, Ricardo Ferreira de Oliveira, Rick Sidwell, Sae1962, Scubanator87, Sesu Prime, Setyon, Skarebo, Ske, Suhrid.suhrid, TakuyaMurata, Thomerow, Thumperward, Tiderolls, Tobias Bergemann, TypeMild, Uncle G, Vadmium, Wbm1058, Wernher, WestwoodMatt, Wtshymanski, Xiaogaozi, Xurin, Yoe, ZeWrestler, Zephalis, 234 anonymous edits

    LicenseCreative Commons Attribution-Share Alike 3.0//creativecommons.org/licenses/by-sa/3.0/

    Signed number representationsHistorySigned magnitude representationOnes' complementTwo's complementExcess-KBase 2Comparison tableOther systems References

    License