我们首先来看一段代码:
#include <iostream>
using namespace std;
int main()
{
int a = 1118;
int n = 9;
cout << "1118的二进制表示第九位是:" << ((a >> n) & 1) << endl;
return 0;
}
我们再来看一段代码:
#include <iostream>
using namespace std;
int main()
{
//<<左移操作
int a = 9;
short b = 9;
unsigned short c = 9;
a <<= 15;
cout << "a=" << hex << a << dec << endl;
b <<= 15;
c <<= 15;
cout << "b=" << b << ",c=" << c << endl;
//>>右移操作
a = 9;
short d = -9;
unsigned short e = 0xffe0; //65504
unsigned char f = 9;
a >>= 2;
d >>= 3;
e >>= 4;
f >>= 3;
cout << a << "," << d << "," << e << "," << int(f) << endl;
return 0;
}
异或操作功能一:将所有的二进制位进行反转操作。如0xFE33^0xFF。
异或操作功能二:不需要临时变量,对两个数进行交换。
#include <iostream>
using namespace std;
int main()
{
int a = 1;
int b = 9;
a = a^b;
b = b^a;
a = a^b;
cout << "a=" << a << " b=" << b << endl;
return 0;
}