r/ProgrammingLanguages • u/Nuoji C3 - http://c3-lang.org • May 03 '26
Blog post Unsigned Sizes: A Five Year Mistake
https://c3-lang.org/blog/unsigned-sizes-a-five-year-mistake/
91
Upvotes
r/ProgrammingLanguages • u/Nuoji C3 - http://c3-lang.org • May 03 '26
View all comments
Show parent comment
1
u/EggplantExtra4946 May 06 '26 edited May 06 '26
Not if all you do is add and multiply unsigned integers. Even if you do subtract unsigned integers and it overflows, the actual bit pattern will be the same as if it were a signed integers and if you use that underflowed unsigned integer to an other unsigned integer, it will produce the correct result, the same result as if you were subtracting and adding signed integers.
Essentially, unsigned integers can be transiently used as signed integers when evaluating an expression whose result will be >= 0 but that can have < 0 temporaries.
And you can more easily check for overflows in unsigned integers without intrinsics than with signed integers. For checking that an addition did not overflowed, you only need to check that the result is superior to both operands of the addition. If you also know that only one of the operand can get big, you only need to check one operand instead of two.
Another benefit of unsigned integers: when compiling a switch statement to a jump table, in the case of a signed integers you need to check that it is < to the length of the jump table and >= 0. Whereas with unsigned integer you only need to check that it is < to the length of the jump table. Of course you can cheat and treat the signed integer as unsigned integer and perform only an upper bound check, but in that case you're implicitly casting the signed integer to unsigned.
What kind of fallacious argument is this? If overflow is a problem then it's a bigger problem for signed integers, at the very least signed integers certainly don't reduces the risk, they obviously increase it.
Unsigned integers are also better for bitwise operations, logical right shift is better than arithmetic right shift and it's a common operation.
Outside of comparisons of signed integers, I'm not convinced you actually signed integers for arithmetic, unsigned integers are enough. Or dare I say, unsigned integers are the superior signed integers. For the processor it's all the same anyway.