3 ms·
Seems like it boils down to using unsigned when you need to treat numbers as fields of bits, and signed when you need to do arithmetic.
by jschwartzi 11y ago
Seems like it boils down to using unsigned when you need to treat numbers as fields of bits, and signed when you need to do arithmetic.
- joe_the_user 11y agoNot experiencing undefined behavior is a desirable thing for arithmetic so unsigned has some appeal for arithmetic too, I'd say.
- jhdevos 11y agoThat seems like a false sense of safety: when doing arithmetic, your program is most likely not going to be handling overflows correctly anyway. Also, when using unsigned ints, any code involving subtractions can lead to subtle bugs that only occur when one value is larger than another. I'd recommend sticking with signed ints for all arithmetic code.
- ridiculous_fish 11y ago> when doing arithmetic, your program is most likely not going to be handling overflows correctly anyway Well with that attitude!
- jschwartzi 11y agoIt might be pretty safe if you're only ever adding, multiplying or dividing with other unsigned numbers. Once you start doing subtraction it could get ugly.