Recently do the problem, often meet the scope is 2^63, take mold 2^64 this topic. When encountering this restriction, you should think of the unsigned long long type.
Can be succinctly declared as a typedef unsigned long long ull. Thus, if an integer of type ull overflows, it is equivalent to modulo 2^64. Because the range of ull is [0,2^64-1].
The ll range is [ -2^63,2^63-1] because the 63rd digit of the sign indicates "positive or negative" and does not represent a value
Here are a few sets of contrast programs to deepen the impression:
(1)
#define _crt_secure_no_warnings #include <iostream> #include <algorithm> #include <string> #include <sstream> #include <set> #include <vector> #include <stack> #include <map> #include < queue> #include <deque> #include <cstdlib> #include <cstdio> #include <cstring> #include < cmath> #include <ctime> #include <functional>using namespace std;typedef long Long ll;typedef unsigned Long Long Ull;int main () {ll a = ~0ll >> 1;ull b = ~0ll >> 1;cout << a << ' << b << E NDL;} /* Output Result:-1 18446744073709551615*/
(2)
#define _crt_secure_no_warnings #include <iostream> #include <algorithm> #include <string> #include <sstream> #include <set> #include <vector> #include <stack> #include <map> #include < queue> #include <deque> #include <cstdlib> #include <cstdio> #include <cstring> #include < cmath> #include <ctime> #include <functional>using namespace std;typedef long Long ll;typedef unsigned Long Long Ull;int main () {ll a = (1LL << +)-1;ull B = (1LL << +)-1;cout << a << ' << b << endl;cout << A + 1 << ' << B + 1 << Endl;} /* Output Result: 9223372036854775807 9223372036854775807-9223372036854775808 9223372036854775808*/
(3)
#define _crt_secure_no_warnings #include <iostream> #include <algorithm> #include <string> #include <sstream> #include <set> #include <vector> #include <stack> #include <map> #include < queue> #include <deque> #include <cstdlib> #include <cstdio> #include <cstring> #include < cmath> #include <ctime> #include <functional>using namespace std;typedef long Long ll;typedef unsigned Long Long Ull;int main () {ull a = (1LL << 1)-1;cout << a << ' << A + << Endl;} /* Output Result: 18446744073709551615 0*/
unsigned long long type and long long