c++ - bitwise right-shift of negative numbers -


this question has answer here:

i looked @ binary representations of negative integers under influence of bit-shift , noticed bitshifting behaves differently these 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 had expected was:

10000000000000000000000000000000 01000000000000000000000000000000 00100000000000000000000000000000 

what behaviour for? or - if there no reason per se - why happen?


Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -