Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is `-1` correct for using as maximum value of an unsigned integer?

Tags:

c++

Is there any c++ standard paragraph which says that using -1 for this is portable and correct way or the only way of doing this correctly is using predefined values?

I have had a conversation with my colleague, what is better: using -1 for a maximum unsigned integer number or using a value from limits.h or std::numeric_limits ?

I have told my colleague that using predefined maximum values from limits.h or std::numeric_limits is the portable and clean way of doing this, however, the colleague objected to -1 being as same portable as numeric limits, and more, it has one more advantage:

unsigned short i = -1; // unsigned short max 

can easily be changed to any other type, like

unsigned long i = -1; // unsigned long max 

when using the predefined value from the limits.h header file or std::numeric_limits also requires to rewrite it too along with the type to the left.

like image 916
Victor Polevoy Avatar asked Dec 07 '17 15:12

Victor Polevoy


People also ask

What is the maximum value of 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.

When should you use the unsigned int?

Unsigned integers are used when we know that the value that we are storing will always be non-negative (zero or positive). Note: it is almost always the case that you could use a regular integer variable in place of an unsigned integer.

What is the minimum value for an unsigned integer?

An unsigned short int , unsigned meaning having no negative sign (-), has a minimum range of 0 and a maximum range of 65,535 .

What is the maximum value of unsigned char in C?

These limits specify that a variable cannot store any value beyond these limits, for example an unsigned character can store up to a maximum value of 255.


1 Answers

Regarding conversions of integers, C 2011 [draft N1570] 6.3.1.3 2 says

Otherwise, if the new type is unsigned, the value is converted by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range of the new type.

Thus, converting -1 to an unsigned integer type necessarily produces the maximum value of that type.

There may be issues with using -1 in various contexts where it is not immediately converted to the desired type. If it is immediately converted to the desired unsigned integer type, as by assignment or explicit conversion, then the result is clear. However, if it is a part of an expression, its type is int, and it behaves like an int until converted. In contrast, UINT_MAX has the type unsigned int, so it behaves like an unsigned int.

As chux points out in a comment, USHRT_MAX effectively has a type of int, so even the named limits are not fully safe from type issues.

like image 134
Eric Postpischil Avatar answered Oct 05 '22 19:10

Eric Postpischil