Explanation of integer expression and integer expression in c ++

Source: Internet
Author: User

Explanation of integer expression and integer expression in c ++

Because we can't remember the range of numbers represented by short, int, long, and long, record them here.

The expression of int is related to the compiler. Generally, the compiler uses four bytes for representation. The four bytes have 32 bits. The first byte is used to indicate positive and negative values, and the other 31 bits are used to represent numerical values. The int value range is (-2 ^ 31,2 ^ 31-1) [Why can a negative number represent one more? in simple words, it is related to the original code, anti-code, and complement code. In the manual Supplementary Code, 1000 0000 indicates-128 ].

Other types of representation range:

Short: 2 bytes (-2 ^ 15 ~ 2 ^ 15-1) that is (-32768 ~ 32767), that is, (SHRT_MIN ~ SHRT_MAZ) (included in the header file );

Int: 4 bytes (-2 ^ 31,2 ^ 31-1) that is (-2147483648,2147483648), that is, (INT_MIN ~ INT_MAZ );

Long: 4 bytes (-2 ^ 31,2 ^ 31-1) that is (-2147483648,2147483648), that is (LONG_MIN ~ LONG_MAZ );

Long: 8 bytes (-2 ^ 63,2 ^ 63-1), that is, (-9223372042554775807,9223372010954775807), that is, (LLONG_MIN ~ LLONG_MAZ );

When the leetcode [204. Count Primes] is used to calculate the number of prime numbers smaller than the integer n, we can see an algorithm called the errado filter method. The general meaning is as follows: first mark the numbers 2 to n-1 as false, and then start traversing. When traversing to I, mark the number of multiples of I as true. When traversing to j, if it is not marked as true, it is a prime number.

I think the most liked SOLUTION IN THE Discuss OF leetcode is as follows:

int countPrimes(int n) {    if (n<=2) return 0;    vector<bool> passed(n, false);    int sum = 1;    int upper = sqrt(n);    for (int i=3; i<n; i+=2) {        if (!passed[i]) {            sum++;            //avoid overflow            if (i>upper) continue;            for (int j=i*i; j<n; j+=i) {                passed[j] = true;            }        }    }    return sum;}

However, I don't think this upper setting is necessary [Well, at that time, I didn't understand what its comment "// avoid overflow" refers ]. Because I think j = I * I, if it is too large, it will be greater than n directly without executing the loop. It has the same function as its continue. However, after I delete the upper sentence, an error is returned when the input is "499979. So I created a local project for debugging and found that there was an illegal access to the array. It was later found that the int indicates that the range is not enough. The size of sqrt (INT_MAX) is 46341. When I> 46341, the int cannot represent this big integer, then it will overflow into a negative number, so it will still be less than n, it will enter the loop, so accessing passed [j] will cause an error. There are two solutions:

1. Use a upper as above, so that you can use an integer;

2. Use the long type to represent j and I * I, that is, the loop is written as for (long j = (long) I * I; j

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.