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?
Signed Integers Are Two’s Complement
11–20 of 126 posts
Re: Signed Integers Are Two’s Complement
#12Earlier 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.
Even today, an implementation may define unsigned overflow.
Re: Signed Integers Are Two’s Complement
#13The 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).
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
#14Earlier 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.
Re: Signed Integers Are Two’s Complement
#15This 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
#16I'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.
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?
...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
#18I'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
#19Earlier 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?
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:
Re: Signed Integers Are Two’s Complement
#20Earlier 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?