-
-
Save iwadon/5098679 to your computer and use it in GitHub Desktop.
-0x80000000と-2147483648とstd::numeric_limits<int>::min()がそれぞれ方が異なるのはなぜ?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <limits> | |
| #include <typeinfo> | |
| #define PRINT_TYPE(x) std::cout << #x << ": " << typeid(x).name() << std::endl | |
| #define PRINT_VALUE(x) std::cout << #x << ": " << typeid(x).name() << "(" << x << ")" <<std::endl | |
| int main() | |
| { | |
| PRINT_TYPE(int); | |
| PRINT_TYPE(long int); | |
| PRINT_TYPE(long long int); | |
| PRINT_TYPE(unsigned int); | |
| PRINT_TYPE(unsigned long int); | |
| PRINT_TYPE(unsigned long long int); | |
| PRINT_VALUE(std::numeric_limits<int>::min()); | |
| PRINT_VALUE(std::numeric_limits<int>::max()); | |
| PRINT_VALUE(-2147483648); | |
| PRINT_VALUE(-0x80000000); | |
| PRINT_VALUE(-0x80000000l); | |
| int v = -2147483648; | |
| PRINT_VALUE(v); | |
| v = -0x80000000; | |
| PRINT_VALUE(v); | |
| return -0x80000000 == -2147483648 ? 0 : 1; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| int: i | |
| long int: l | |
| long long int: x | |
| unsigned int: j | |
| unsigned long int: m | |
| unsigned long long int: y | |
| std::numeric_limits<int>::min(): i(-2147483648) | |
| std::numeric_limits<int>::max(): i(2147483647) | |
| -2147483648: l(-2147483648) | |
| -0x80000000: j(2147483648) | |
| -0x80000000l: l(-2147483648) | |
| v: i(-2147483648) | |
| v: i(-2147483648) | |
| [1] 35925 exit 1 ./a |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment