I looked at the binary representations of negative integers under the influence of the bit-shift and I noticed that bitshifting behaves differently for these than for positive ones.
int imax = std::numeric_limits<int>::max();
std::bitset<32> bs0(imax+1);
std::cout << bs0 << "\n";
std::bitset<32> bs1((imax+1) >> 1);
std::cout << bs1 << "\n";
std::bitset<32> bs2((imax+1) >> 2);
std::cout << bs2 << "\n";
Output:
10000000000000000000000000000000
11000000000000000000000000000000
11100000000000000000000000000000
What I had expected was:
10000000000000000000000000000000
01000000000000000000000000000000
00100000000000000000000000000000
What is this behaviour for? Or - if there is no reason per se for it - then why does it happen?