Could someone explain this behavior?
#include <iostream>
using namespace std;
int main(){
for(int i = 0; i <= 32; i++){
unsigned long test = 1u << i;
cout << test << endl;
}
}
outputs:
1
2
4
8
16
32
64
128
256
512
1024
2048
4096
8192
16384
32768
65536
131072
262144
524288
1048576
2097152
4194304
8388608
16777216
33554432
67108864
134217728
268435456
536870912
1073741824
2147483648
1
Which makes sense until the end. Bit shift operators shouldn't wrap, but here it appears to be doing exactly that. I imagine it's because the literal '1u' is a smaller type than an unsigned long. Using '1ul' instead makes the behavior perfectly normal, so there must be something going on with the type conversion, but I'm curious to know exactly what and why!