#include <iostream>
using namespace std;
int main() {
unsigned long P;
P = 0x7F << 24;
cout << P << endl;
P = 0x80 << 24;
cout << P << endl;
return 0;
}
2130706432
18446744071562067968
As you can see, the first result is correct. But the second result is extremely wrong. The expected result is 2147483648 and it does not match with 18446744071562067968.
I want to know why
The type of the expression 0x80 << 24 is not unsigned long, it’s int. You then assign the result of that expression to P, and in the process convert it to an unsigned long. But at that point it has already overflown (incidentally causing undefined behaviour). Use unsigned long literals in your expression:
P = 0x80ul << 24;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With