3

I'm trying to understand the differences between these two systems and their impact on C programming.

From what I've learned from Wikipedia:

  1. both systems are used to represent negative numbers
  2. one's complement applies bitwise NOT to negative number (the system has +0 and -0)
  3. two's complement does as in step 2 and adds 1 (eliminates +/-0)

Am I missing something else?

My questions:

  1. which architectures support which system? What is the most common these days (1's or 2's complement)?
  2. in what sense should we consider these systems when programming in C? Does it mainly make sense only in embedded world?

Thanks in advance!

Josh Lee
  • 171,072
  • 38
  • 269
  • 275
Mark
  • 1,751
  • 3
  • 14
  • 14

2 Answers2

4

Most systems nowadays use two's complement, since it lets the computer do the same exact operation for addition/subtraction without caring about the particular sign of the number.

When you're programming, the arithmetic works regardless of the system used -- the range of the data types are defined by the language, so if it says a type will work in the range -2^31 to +2^31 - 1, then it'll work regardless of the notation. You need to be careful when working with individual bits or bit shifts, though -- those won't behave like power-of-two arithmetic in non-two's complement systems (although you're not too likely to encounter such systems, and probably never will, if you're just working with PCs).

user541686
  • 205,094
  • 128
  • 528
  • 886
0

The only advantage of ones'-complement notation for integers is that it allows conversions to and from sign-magnitude form to be performed without a carry chain. Building a computer with a set of blinkenlights that show each register's value in sign-magnitude form will be much more convenient if the registers use ones'-complement form than if they use two's-complement form. If one wanted to use separate storage latches for the blinkenlights and the CPU's registers, the easiest way to accommodate things would be to have one circuit which translates two's-complement to one's-complement or sign-magnitude form, and then have each register write simultaneously store the two's-complement value in the register while updating the blinkenlight latches with the sign-magnitude value. Latching circuitry is sufficiently expensive, however, that if registers are being built out of discrete latches anyway, adding some circuitry to the ALU to make it use ones'-complement, and then feeding the lights from the CPU's "real" registers, may be cheaper than including an extra set of latches for the lights.

Over the last few decades, of course, the relative costs of different circuit elements have shifted to the point that it would be absurd to have lights wired to directly report the state of a CPU's registers. Consequently, the practical advantages that ones'-complement designs might have had in the past are no longer applicable.

supercat
  • 77,689
  • 9
  • 166
  • 211