5 ms·
>Unsigned does not mean "not negative", it means "modular arithmetic". Signed is modular arithmetic with an offset. An unsigned char called i can be thought o
by AnotherGoodName 5y ago
>Unsigned does not mean "not negative", it means "modular arithmetic".
Signed is modular arithmetic with an offset.
An unsigned char called i can be thought of as "i mod 256"
A signed char called i can be thought of as "((i+128) mod 256) - 128"
All results for over/underflow hold for 'i' cast into a char if you do the above in both cases.
I think the premise of the article is quite reasonable. It's slightly easier to reason about over/underflow for unsigned arithmetic since it's not offset.
- jcelerier 5y ago> 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.
- AnotherGoodName 5y agoSigned 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.
- jcelerier 5y ago> 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.
- deleted 5y ago[deleted]