3 ms·
> A negative number of bytes is nonsense. That's true, but mixing signed and unsigned integers opens a much bigger can of worms (for instance it's not unusual
by flohofwoe 3y ago
> A negative number of bytes is nonsense.
That's true, but mixing signed and unsigned integers opens a much bigger can of worms (for instance it's not unusual to add a negative quantity to an unsigned integer). It's often better to just stick to signed integer types which are big enough to cover the required positive range (e.g. you only loose one bit, and 63 bits ought to be enough for anybody).
...also an optimizing compiler will usually turn something like:
assert((index >= 0) && (index < size));
...into a single comparison, since on the assembly level it's free to interpret that 'index' as unsigned.
- bjourne 3y agoIt's not about the compiler, it's about clearly communicating the intent of the code to the reader.