geza
geza

Reputation: 29942

When converting to unsigned, the standard says "the least unsigned integer" is the result. Why does "least" matter here?

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

Answers (1)

Yakk - Adam Nevraumont
Yakk - Adam Nevraumont

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

Related Questions