3
\$\begingroup\$

I have only seen mention 2's complement practically everytime signed number representation is mentioned. However, I have found that there are other ways to represent signed numbers also, these are 1's complement, excess-k and base -2.

Except 1's complement I do not really understand how the other two work. However, what I want to know is, are these other representations ever used at all? If so, where and why? I know that the strength of 2's complement is that addition and subtraction become the same. However, I am not sure about the strengths of the other mentioned methods.

\$\endgroup\$
  • \$\begingroup\$ You need a textbook, I think. \$\endgroup\$ – TisteAndii Nov 23 '16 at 1:14
  • \$\begingroup\$ which textbook? \$\endgroup\$ – quantum231 Nov 23 '16 at 1:21
  • \$\begingroup\$ In the dominant IEEE P754 standard for floating point numbers, sign&magnitude is used for the mantissa (aka significand) and excess- for the exponent. S&M is a bit simpler with rounding, has no asymmetry (-128... +127 in 8bits two's complement, which is a pain for operations as multiplications and divisions...), how while excess- allows continuity between small exponents and zero which is represented with zeros. \$\endgroup\$ – TEMLIB Nov 23 '16 at 1:55
  • \$\begingroup\$ There were a lot of different ideas tried in the past. But a huge advantage for twos complement comes in the implementation details of add/subtract hardware logic. Some early Russian computers even used balanced trinary. But that was a long time back, as well. The world has explored the options and, for mainstream computing work, has settled on twos complement notation. For integers. Floating point is a different problem, entirely. Normalization, easy ordering, and other factors play there. So it's handled differently. You probably do need an historical book for details about 'why'. \$\endgroup\$ – jonk Nov 23 '16 at 2:33
  • 1
    \$\begingroup\$ The Apollo Guidance Computer used 1's complement IIRC. \$\endgroup\$ – Wossname Nov 23 '16 at 9:15
5
\$\begingroup\$

We all float on again...right back into the land of sign-magnitude numbers!

Twos' complement is almost universally used for integer arithmetic for the exact reasons you stated -- addition and subtraction work the same way, carries behave sensibly, and signed overflow is easy to detect as well.

However, floating point math almost universally uses sign-magnitude representation for the overall number, primarily to ease rounding considerations and remove asymmetries in significand range. The only machines that don't use sign-magnitude representation for floating point are of the prehistoric type (even things like S/360s, VAXen, and Crays that predate IEEE 754 use sign-magnitude FP). However, the ubiquity of sign-magnitude IEEE 754 FP has had an impact on integer arithmetic as well, thanks to Brendan Eich's little project called JavaScript.

You see, JavaScript's only arithmetic type is the IEEE 754 double precision float; thankfully, IEEE 754 floats can represent integers exactly up to the significand width. The one problem with this, though, is that using IEEE 754 floats for integer math means you are stuck with sign-magnitude integer math! That's right, negative integers work differently in JavaScript than in every other modern language. Think about this the next time you hear someone pitching Node.js...

\$\endgroup\$
  • \$\begingroup\$ Is sign extension when shifting the only important issue to be careful about when using 2's complement or there are others too? I am talking about special considerations here which if not kept in mind will cause failure. \$\endgroup\$ – quantum231 Nov 23 '16 at 12:38
  • \$\begingroup\$ Sign extension on right shifts is pretty much it, unless you're implementing a signed multiply/divide block... \$\endgroup\$ – ThreePhaseEel Nov 23 '16 at 12:39
  • \$\begingroup\$ Yes, I need to carry out multiplication. \$\endgroup\$ – quantum231 Nov 23 '16 at 12:55
  • \$\begingroup\$ Hmm it seems I need sign extension of operands to twice the operand bit length before doing multiplication \$\endgroup\$ – quantum231 Nov 23 '16 at 12:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.