C ++ 14 integer maximum/small value
Table of Contents1. how to obtain the maximum value 1.1. C ++ 14 method 1.2. How to achieve the maximum value 1.3 By yourself. How to Implement the minimum value 1 and how to obtain the maximum value 1.1 C ++ 14
Std: cout <"int \ t" <std: numeric_limits
: Lowest () <'\ t' <std: numeric_limits
: Max () <'\ n ';
Output result:
Int-21474836482147483647
1.2 How to achieve the maximum value by yourself
Int max_int = (int) (unsigned )~ 0> 1); cout <max_int <endl;
Output result:
2147483647
The results are the same. This line of code needs to be interpreted:
~ 0 is the inverse, and all bits are set to 1 (unsigned) transformation is to interpret the first from the left as a non-Signed bit, prepare for the next right shift> 1 is to shift one digit to the right (divided by 2). Because it is an unsigned integer, add 0 on the left and convert the result to the destination int type because there is no overflow, so we can certainly achieve the minimum value of 1.3.
Int min =-(int) (unsigned )~ 0> 1)-1;
Just add the symbol on the basis of the calculated maximum value and subtract one.
Author: dean
Created:
Validate