4 ms·
Why does this implementation used signed integers for indices at all?
by person_of_color 7y ago
Why does this implementation used signed integers for indices at all?
- MauranKilom 7y agoJava. There are no unsigned integers in Java. Many would consider that a benefit (although opinions vary on signed vs unsigned bytes). FWIW, the creators of C++ have stated that they'd go with signed integers for e.g. indexes in the library interfaces nowadays (but it's way too late to change that). Popular problem with using unsigned : Write a reverse for loop from n (exclusive) to 0 (inclusive). See if you can do it without discarding your two most intuitive (but wrong) approaches.
- smellia 7y agoIts the opposite. Its easy to write such a loop with unsigned ints: for(x=top;x<=top;x--). However you can't reliably write a loop spanning the whole space with signed ints without breaking the no-overflow rules. With unsigned you can do x=0;do{stuff(x);}while(++x); Good luck doing this with signed ints.
- mypalmike 7y agoThis is why integer sign should be part of the operation rather than the type. Every language but assembly gets this wrong.