Reputation: 29942
C++ standard says in [conv.integral/2], about integer conversion to unsigned:
If the destination type is unsigned, the resulting value is the least unsigned integer congruent to the source integer (modulo 2n where n is the number of bits used to represent the unsigned type).
My question is, why the word "least" is there? Is it possible that multiple results are possible, and we need to choose one from them?
Upvotes: 8
Views: 379
Reputation: 275270
There are an infinite number of integers equal to any value k modulo 2n. There is k, k+2n, k+2*2n, k+3*2n, k-2n, k-2*kn, etc.
Of these, one is the least unsigned (positive) value.
Parts of the C++ standard are specified in math. I believe this is one of them.
Upvotes: 8