• 位运算(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

  • 相关阅读:
    剑指Offer-合并两个排序的链表
    1. Two Sum&&15. 3Sum&&18. 4Sum
    Sumo生成数据
    357. Count Numbers with Unique Digits
    553. Optimal Division
    147. Insertion Sort List
    24. Swap Nodes in Pairs
    【LeetCode & 剑指offer刷题】发散思维题3:62 圆圈中最后剩下的数字(约瑟夫环问题)
    【LeetCode & 剑指offer刷题】发散思维题2:43 n个骰子的点数
    【LeetCode & 剑指offer刷题】发散思维题1:17 打印从1到最大的n位数
  • 原文地址:https://www.cnblogs.com/wanghao-boke/p/11304991.html
Copyright © 2020-2023  润新知