C ++ 14 integer maximum/small value

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.