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?
An unsigned integer is a 32-bit datum that encodes a nonnegative integer in the range [0 to 4294967295]. The signed integer is represented in twos complement notation. The most significant byte is 0 and the least significant is 3.
Unsigned Integers (often called "uints") are just like integers (whole numbers) but have the property that they don't have a + or - sign associated with them. Thus they are always non-negative (zero or positive). We use uint's when we know the value we are counting will always be non-negative.
A minimum integer value that can be stored in an unsigned int data type is typically 0.
Difference between Signed Int and Unsigned IntA signed int can store negative values. Unsigned integer values can only store positive values. A signed integer can get an overflow error while used in a program with larger values.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With