Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

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?

like image 269
geza Avatar asked Nov 07 '18 00:11

geza


People also ask

What is the lowest unsigned integer?

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.

What is special about an unsigned integer?

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.

What is the lowest value for unsigned variable?

A minimum integer value that can be stored in an unsigned int data type is typically 0.

What is the difference between unsigned and unsigned int?

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.


1 Answers

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.

like image 92
Yakk - Adam Nevraumont Avatar answered Oct 04 '22 10:10

Yakk - Adam Nevraumont