Live data from Hacker News

Signed Integers Are Two’s Complement

open-std.org

11–20 of 126 posts

Re: Signed Integers Are Two’s Complement

#11
post #3

I'm curious why some old architectures didn't use two's complement for signed numbers. What advantage did one's complement or signed magnitude have over two's complement?

In addition to what's mentioned in the already great sibling comments, it's worth noting that IEEE floating point is signed-magnitude.

Re: Signed Integers Are Two’s Complement

#12

Earlier quoted context omitted.

> on x86/amd4 sizeof(int) will always be equal to 4 Nothing is stopping your C compiler from making the guarantee sizeof(int)=4 on x86/amd64.

I think you are in agreement with the comment you are replying to.

The comment suggested the standard make it implementation defined rather than undefined. There's not a meaningful difference here.

Even today, an implementation may define unsigned overflow.

Re: Signed Integers Are Two’s Complement

#13

The real issue isn't that C doesn't have a standard int overflow, but that it's undefined. What they could have done is made it implementation defined , like sizeof(int), which depends on the implementation (hardware) but on the other hand isn't undefined behavior (so on x86/amd4 sizeof(int) will always be equal to 4).

It's undefined for a reason.

  size_t size = unreasonable large number;
  char buf = malloc (size);
  char *mid = buf + size / 2;
  int index = 0;
  for (size_t x = 0; x 
A common optimization by a compiler is to introduce a temporary

  char *temp = mid + index;
prior to the loop and then replace the body of the loop with

  *(temp++) = x;
If the compiler has to worry about integer overflow, this optimization is not valid.

(I'm not a compiler engineer. Losing the optimization may be worth-while. Or maybe compilers have better ways of handling this nowadays. I'm just chiming in on why int overflow is intentionally undefined in the Fine Standard)

Re: Signed Integers Are Two’s Complement

#14

Earlier quoted context omitted.

I think you are in agreement with the comment you are replying to.

The comment suggested the standard make it implementation defined rather than undefined. There's not a meaningful difference here. Even today, an implementation may define unsigned overflow.

Oh, I see, I wonder if greenhouse_gas is suggesting a feature similar to sizeof() that can be used to portably adapt your program's design to the target's overflow capability.

Re: Signed Integers Are Two’s Complement

#15
> Overflow in the positive direction shall wrap around

This appears to be defining signed integer overflow semantics, which prevents the compiler from doing certain basic optimizations, for example, that (x*2)/2 == x. Is that part of this? Has anyone measured the perf cost on a real program?

Re: Signed Integers Are Two’s Complement

#16
post #3

I'm curious why some old architectures didn't use two's complement for signed numbers. What advantage did one's complement or signed magnitude have over two's complement?

It can be useful to distinguish between positive and negative zero in some cases, for example when dealing with values that have been rounded to zero or limits approaching zero.

Was that ever really a reason for signed magnitude, or did people just make use of the 2nd representation of zero because because it was available and they couldn't be bothered putting that information in another variable or using floating point or fixed point, or anything else that would have achieved the same result?

Re: Signed Integers Are Two’s Complement

#17

> Overflow in the positive direction shall wrap around This appears to be defining signed integer overflow semantics, which prevents the compiler from doing certain basic optimizations, for example, that (x*2)/2 == x. Is that part of this? Has anyone measured the perf cost on a real program?

It prevents risky optimisations; this now requires the compiler to prove that such optimisations won't change the semantics of the code, e.g. in your case by essentially proving that the high 2 bits of x (only 1 in the unsigned case, due to sign-extension) will never be set.

...and it could be argued that if the compiler couldn't prove that was true, then it just helped you find a possible overflow bug in the code. If you actually wanted the compiler to unconditionally assume (x*2)/2==x and optimise accordingly, then you'd have to tell it such; e.g. MSVC has the __analysis_assume() feature.

Re: Signed Integers Are Two’s Complement

#18
post #3

I'm curious why some old architectures didn't use two's complement for signed numbers. What advantage did one's complement or signed magnitude have over two's complement?

In addition to what's mentioned in the already great sibling comments, it's worth noting that IEEE floating point is signed-magnitude.

Is there a good way to represent floating points in order to do complement arithmetic?

Re: Signed Integers Are Two’s Complement

#19
post #16

Earlier quoted context omitted.

It can be useful to distinguish between positive and negative zero in some cases, for example when dealing with values that have been rounded to zero or limits approaching zero.

Was that ever really a reason for signed magnitude, or did people just make use of the 2nd representation of zero because because it was available and they couldn't be bothered putting that information in another variable or using floating point or fixed point, or anything else that would have achieved the same result?

I have a feeling signed-magnitude predates binary and complement arithmetic --- it is, after all, the "natural" way humans work with numbers. A lot of the early non-binary computers used some form of sign-magnitude, all the way back to punch card formats:

https://en.wikipedia.org/wiki/Signed_overpunch

On the other hand (no pun intended), early mechanical (decimal) manual adding machines made use of complement arithmetic too:

https://en.wikipedia.org/wiki/Comptometer

https://en.wikipedia.org/wiki/Method_of_complements

Re: Signed Integers Are Two’s Complement

#20

Earlier quoted context omitted.

In addition to what's mentioned in the already great sibling comments, it's worth noting that IEEE floating point is signed-magnitude.

Is there a good way to represent floating points in order to do complement arithmetic?

Yes, see JavaScript: https://www.w3schools.com/js/js_numbers.asp
Post reply on HN