Представляет наибольшее возможное значение типа Int32. Represents the largest possible value of an Int32. Это поле является константой. This field is constant.
В следующем примере MaxValue свойство используется для OverflowException предотвращения преобразования в Int32 значение. The following example uses the MaxValue property to prevent an OverflowException when converting to an Int32 value.
Значение этой константы равно 2 147 483 647; то есть шестнадцатеричный 0x7FFFFFFF. The value of this constant is 2,147,483,647; that is, hexadecimal 0x7FFFFFFF.
Is there any code to find the maximum value of integer (accordingly to the compiler) in C/C++ like Integer.MaxValue function in java?
9 Answers 9
std::numeric_limits is a template type which can be instantiated with other types:
I know it’s an old question but maybe someone can use this solution:
So far we have -1 as result ’till size is a signed int.
As Standard says, bits that are shifted in are 1 if variable is signed and negative and 0 if variable would be unsigned or signed and positive.
As size is signed and negative we would shift in sign bit which is 1, which is not helping much, so we cast to unsigned int, forcing to shift in 0 instead, setting the sign bit to 0 while letting all other bits remain 1.
We could also use a mask and xor but then we had to know the exact bitsize of the variable. With shifting in bits front, we don’t have to know at any time how many bits the int has on machine or compiler nor need we include extra libraries.
Most of the times, in competitive programming, there is a need to assign the variable, the maximum or minimum value that data type can hold, but remembering such a large and precise number comes out to be a difficult job. Therefore, C++ has certain macros to represent these numbers, so that these can be directly assigned to the variable without actually typing the whole number.
INT_MAX is a macro that specifies that an integer variable cannot store any value beyond this limit.
INT_MIN specifies that an integer variable cannot store any value below this limit.