/*lesson 4*/
#include <iostream> //头文件☆文件后无需添加.h
#include<iomanip>
using namespace std;
int main()
{
int X = 3, Y = 5;
//按位与运算
cout << "按位与运算";
cout << X << "&" << Y << " = " << (X & Y) << endl;
cout << setw(10) << "0000 0011" << endl;
cout << setw(1) << "&" << setw(9) << "0000 0101" << endl;
cout << setw(10) << "----------" << endl;
cout << setw(10) << "0000 0001" << endl;
//按位与应用 取低字节
cout << "按位与应用:取出0x1111低字节内容" << endl;
cout << "0x1111 & 0x00FF" << " = " << (0x1111 & 0x00FF) << endl;
cout << endl; //换行
//按位或运算
cout << "按位或运算";
cout << X << "|" << Y << " = " << (X | Y) << endl;
cout << setw(10) << "0000 0011" << endl;
cout << setw(1) << "|" << setw(9) << "0000 0101" << endl;
cout << setw(10) << "----------" << endl;
cout << setw(10) << "0000 0111" << endl;
//按位或应用 低字节全部置1
cout << "按位或应用:低字节全部置1" << endl;
cout << "0x0000 & 0x00FF" << " = " << (0x0000 | 0x00FF) << endl;
cout << endl; //换行
//按位取反运算
cout << "按位取反运算";
cout << "~" << X << "=" << ~X << endl;
cout << setw(10) << "0000 0011" << endl;
cout << setw(1) << "~" << setw(9) << "1111 1100" << endl;
cout << endl; //换行
//按位异或运算
cout << "按位异或运算";
cout << X << "^" << Y << " = " << (X ^ Y) << endl;
cout << setw(10) << "0000 0011" << endl;
cout << setw(1) << "^" << setw(9) << "0000 0101" << endl;
cout << setw(10) << "----------" << endl;
cout << setw(10) << "0000 0110" << endl;
//按位异或应用 将低数位翻转
cout << "按位异或应用:将低数位翻转" << endl;
cout << "0xFA ^ " << "0x0F" << "=" << (0xFA ^ 0x0F) << endl;
cout << endl; //换行
system("pause"); //程序暂停
return 0;
}
运行结果: