学习C/C++同步进行 第4课 & | ~ ^

/*lesson 4*/
#include <iostream>                           
#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;
}

运行结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值