c++ - bitwise right-shift of negative numbers -
this question has answer here:
- right shifting negative numbers in c 6 answers
- arithmetic bit-shift on signed integer 4 answers
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
Post a Comment