unsigned long long type and long long

Source: Internet
Author: User

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

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.