3 ms·
> Unsigned does not mean "not negative", it means "modular arithmetic". In which case it has a terrible name. The name clearly indicates that the primary featu
by ghusbands 5y ago
> Unsigned does not mean "not negative", it means "modular arithmetic".
In which case it has a terrible name. The name clearly indicates that the primary feature is that it is not signed, and the logical conclusion from that is that it is not negative.
That said, I agree; using signed types in C/C++ does generally seem safer and a majority of the techniques listed in the article are unsafe, logically confused, and/or inhibit optimisation. Using signed types in languages like C# is also generally more pleasant, because otherwise you end up with a lot of casts just to interact with the base types (like arrays), language features and third-party libraries.
- Noughmad 5y ago> The name clearly indicates that the primary feature is that it is not signed, and the logical conclusion from that is that it is not negative. No, the logical conclusion is that since it has no sign, you don't know whether it's positive or negative. Otherwise the name would be "positive" or "non-negative", not "unsigned". And this conclusion would also be correct.
- xxs 5y ago>The name clearly indicates that the primary feature is that it is not signed, and the logical conclusion from that is that it is not negative. Years ago, when the names were designated the CPUs didn't even have proper signed instructions. There was a sign flag, and that was all. In that regard the unsigned was the natural CPU sympathetic type.
- int_19h 5y agoAnd yet the default type for variables and function arguments in C was (before it got deprecated) signed int, not unsigned.
- xxs 5y agosigned/unsigned stuff predates C, though. My (lack of) experience with C came much later, when I had used assembly for 3 different CPUs already, so no recollection of the times.
- int_19h 5y agoIf you mean the default being signed, it's there because B - the predecessor of C - only had a signed integer (machine word) type. But note that this is about the case when the variable has no type at all - e.g. "auto x" is a signed int. They still had the choice of "int x" being equivalent to either "signed int" or "unsigned int" by default, though - since there's no backwards compatibility issue there - and chose signed. FWIW, signed seems to be the default for most other languages from that time period, and often the only option - e.g. Algol-60 didn't have unsigned integers at all. Pascal kinda sorta did, if you defined an integer subtype with 0 as the lower bound... but the upper would still be that of signed int, and it'd behave as such. And Algol-68 called its unsigned type BITS - a pretty strong hint that they didn't think of it as arithmetic. I'm actually kinda curious now as to when the concept of unsigned integers first appeared in a high-level programming language first.
- SAI_Peregrinus 5y agoSigned also tends to mean modular arithmetic. It's just with a different definition of modular arithmetic than the Euclidean one. See Boute's 1992 paper "The Euclidean Definition of the Functions div and mod" [1] for details on the various definitions. Both signed and unsigned arithmetic are using finite fields, instead of Abelian rings as most people expect. [1] https://biblio.ugent.be/publication/314490/file/452146.pdf https://biblio.ugent.be/publication/314490/file/452146.pdf
- jacobolus 5y ago> Both signed and unsigned arithmetic are using finite fields, This is not correct. They are rings with some additional (not mathematically standard) operations thrown in. 'Finite field' means something very specific, and quite different. https://en.wikipedia.org/wiki/Finite_field https://en.wikipedia.org/wiki/Finite_field Computer integer arithmetic hardware can be used to implement finite field arithmetic, but even the 2^n case takes quite a bit of trickery. https://en.wikipedia.org/wiki/Finite_field_arithmetic https://en.wikipedia.org/wiki/Finite_field_arithmetic