zoukankan      html  css  js  c++  java
  • 位运算(C++)

    C++输出十六进制

    #include<iostream>
    #include<iomanip>
    using namespace std;
    
    int main()
    {
        int a = 60;
        int b = 13;
        int c =  a &b;
        cout << "a : hex "<<hex << a << endl;
        cout << "b : hex "<<hex << b << endl;
        cout << "c : hex "<<hex << c << endl;
        return 0;
    }

    运行结果:

    exbot@ubuntu:~/wangqinghe/C++/enum$ ./mybyte

    a : hex 3c

    b : hex d

    c : hex c

    C++输出二进制:

    #include<iostream>
    #include<bitset>
    using namespace std;
    const int num = 8; //输出位数控制
    int main()
    {
        int n_max = 42;
        cout << (bitset<num>)n_max << endl;
        return 0;
    }

    运行结果:

    exbot@ubuntu:~/wangqinghe/C++/enum$ ./binbyte

    00101010

    位计算:

    #include<iostream>
    #include<bitset>
    using namespace std;
    const int num = 8;
    
    int main()
    {
        int a = 60;
        int b = 13;
        int c = 0;
         cout << "a = " << a << endl; 
        cout << "b = " << b << endl;
    
        c = a & b;
        cout << "& AND" << endl;
        cout << "a : " << (bitset<num>)a << endl;
        cout << "b : " << (bitset<num>)b << endl;
        cout << "c : " << (bitset<num>)c << endl;
    
        c = a | b;
        cout << "| OR" << endl;
        cout << "a : " << (bitset<num>)a << endl;
        cout << "b : " << (bitset<num>)b << endl;
        cout << "c : " << (bitset<num>)c << endl;
    
        c = a ^ b;
        cout << "^ XOR" << endl;
        cout << "a : " << (bitset<num>)a << endl;
        cout << "b : " << (bitset<num>)b << endl;
        cout << "c : " << (bitset<num>)c << endl;
    
        c = (~a);
        cout << "~ Reverse" << endl;
        cout << "a = " << a << endl;
        cout << "c = " << c << endl;
        cout << "a : " << (bitset<num>)a << endl;
        cout << "c : " << (bitset<num>)c << endl;
    
        c = a << 2;
        cout << "<< shift left" << endl;
        cout << "a = " << a << endl;
        cout << "c = " << c << endl;
        cout << "a : " << (bitset<num>)a << endl;
        cout << "c : " << (bitset<num>)c << endl;
    
        c = a >> 2;
        cout << "<< shift right" << endl;
        cout << "a = " << a << endl;
        cout << "c = " << c << endl;
        cout << "a : " << (bitset<num>)a << endl;
        cout << "c : " << (bitset<num>)c << endl;
        return 0;
    }

    与预算:有0 为0,双1为1

    或运算:有1为1,双0 为0

    异或运算:相同为0,不同为1

  • 相关阅读:
    关于屏幕点亮和熄灭你所需要知道的
    关于handler的使用和理解
    关于Android Task的学习
    Android触摸屏幕事件总结
    Android工作问题总结
    Android生命周期总结
    Android中如何在子线程更新UI
    Eclipse中启动tomcat无效,而手动启动可以访问的原因
    使用Spring进行文件加载进内存
    spring集成quartz定时器的使用
  • 原文地址:https://www.cnblogs.com/wanghao-boke/p/11304991.html
Copyright © 2011-2022 走看看