整数左移右移补码都是补0,今天才发现负数的补码左移和右移不一样!
结论:负数的补码左移是右边补0,右移是左边补1!!!
上例子:
#include<vector>
#include<iostream>
#include<bitset>
using namespace std;
int main() {
int n = -1;
int m = -1;
cout << "n=" << n << " m=" << m << endl;
cout <<"原始n= " <<bitset<32>(n) << endl;
n = n >> 10;
cout << "右移10位后n=" << bitset<32>(n) << endl;//负数右移左边补1
cout << "原始m= " << bitset<32>(m) << endl;
m = m << 10;
cout << "左移10位后m=" << bitset<32>(m) << endl;//负数左移左边补0
system("pause");
}
输出: