Why does 1<<31 print 18446744071562067968 as output when we run the following code?
#include<iostream>
using namespace std;
int main(){
unsigned long long int i = 1<<31;
cout<<i; // this prints 18446744071562067968
}
Why does 1<<31 print 18446744071562067968 as output when we run the following code?
#include<iostream>
using namespace std;
int main(){
unsigned long long int i = 1<<31;
cout<<i; // this prints 18446744071562067968
}
1 is a signed int, which on your system is 32 bits. 1 << 31 results in overflow, and is a negative number (0x80000000). This, when converted to a 64 bit unsigned long long is then sign extended to 64 bits before being converted to the ULL value, which is 0xFFFFFFFF80000000, or the large decimal number you see.