News for hackers. Stuff that matters.
Re:Almost Always Unsigned by jcelerier on Sunday January 02, 2022 @05:26AM (#29767496)
> Signed is modular arithmetic with an offset.

it is not, it models integers (Z). The model only works in the bounds of what the platform can offer of course ; when you use "int" you say "this is an integer. In any case I'm supposed to encounter, adding two positive integers will yield a greater positive integer ; if I add numbers so big that my platform cannot represent the sum my program is meaningless anyways".

It is very unlike unsigned which models (much more accurately since it's much easier for our computers with finite memory) Z/pZ.

Re:Almost Always Unsigned by AnotherGoodName on Sunday January 02, 2022 @05:31AM (#29767525)
Signed and unsigned are actually treated internally the same on computers.

The reason you see -2 printed out when you add signed chars -1 and -1 together is because adding 255 and 255 together gives 254 under mod 256. The signed model that allows negative numbers relies on the fact that all numbers on computers are modular arithmetic.

So in that sense any argument that unsigned is modular and signed isn't is incorrect. You're better off choosing the one that forces you to consider how the computer actually operates under the hood.

Re:Almost Always Unsigned by jcelerier on Sunday January 02, 2022 @05:37AM (#29767570)
> Signed and unsigned are actually treated internally the same on computers.

if you are writing code in C or C++ the computer you are programming for is the C / C++ abstract machine. C++ recently sanctified complement-of-two as integer representation, but C does not and supports machines with one's complement representation.

Re:Almost Always Unsigned by leni536 on Sunday January 02, 2022 @05:47AM (#29767633)
> C++ recently sanctified complement-of-two as integer representation

While that's true, signed integer overflow on arithmetic operations remains undefined behavior.

Re:Almost Always Unsigned by kryptiskt on Sunday January 02, 2022 @06:57AM (#29768011)
One's complement is on its way out in C too. The next C standard will require two's complement.