位运算

  • 内存的基本单元是字节,一个字节有8个位

一 位与 &

image
对应位, 都是1, 结果才是1

#include <iostream>
#include <Windows.h>
#include <bitset>

using namespace std;

int main()
{
    int num1 = 0xa3;      // 1010 0011
    int num2 = 0x2f;      // 0010 1111
    const int width = 8;  // width表示bitset声明的宽度,即有输出的二进制位数。

    cout << (bitset<width>)num1 << " & " << endl;
    cout << (bitset<width>)num2 << endl;
    cout << "---------" << endl;
    cout << (bitset<width>)(num1 & num2) << endl;
    cout << "十六进制:0x" << hex << (num1 & num2) << endl;

    system("pause");
    return 0;
}

image

二 位或 |

image
对应位, 都是0, 结果才是0
只要有一位是1, 结果就是1

#include <iostream>
#include <Windows.h>
#include <bitset>

using namespace std;

int main()
{
    int num1 = 0xa3;      // 1010 0011
    int num2 = 0x2f;      // 0010 1111
    const int width = 8;  // width表示bitset声明的宽度,即有输出的二进制位数。

    cout << (bitset<width>)num1 << " | " << endl;
    cout << (bitset<width>)num2 << endl;
    cout << "---------" << endl;
    cout << (bitset<width>)(num1 | num2) << endl;
    cout << "十六进制:0x" << hex << (num1 | num2) << endl;

    system("pause");
    return 0;
}

image

三 位非 ~

image
按位取反

#include <iostream>
#include <Windows.h>
#include <bitset>

using namespace std;

int main()
{
    int num = 0xa3;        // 1010 0011
    const int width = 32;  // width表示bitset声明的宽度,即有输出的二进制位数。

    cout << (bitset<width>)num << " ~ " << endl;
    cout << "---------------------------------" << endl;
    cout << (bitset<width>)(~num) << endl;
    cout << "十六进制:0x" << hex << (~num) << endl;

    system("pause");
    return 0;
}

image

四 异或 ^

image
相同为0,不同为1

#include <iostream>
#include <Windows.h>
#include <bitset>

using namespace std;

int main()
{
    int num1 = 0xa3;      // 1010 0011
    int num2 = 0x2f;      // 0010 1111
    const int width = 8;  // width表示bitset声明的宽度,即有输出的二进制位数。

    cout << (bitset<width>)num1 << " ^ " << endl;
    cout << (bitset<width>)num2 << endl;
    cout << "---------" << endl;
    cout << (bitset<width>)(num1 ^ num2) << endl;
    cout << "十六进制:0x" << hex << (num1 ^ num2) << endl;

    system("pause");
    return 0;
}

image

五 左移 <<

右边的位用 0 来填充, 移动超出左边边界的位则直接抛弃。
向左移动 n个位置,就等同于乘以 2的n次方

#include <iostream>
#include <Windows.h>
#include <bitset>
using namespace std;

int main()
{
    cout << (bitset<8>)10 << " << 3" << endl;
    cout << (bitset<8>)(10 << 3) << endl;

    system("pause");
    return 0;
}

image

六 右移 >>

如果左操作数是无符号类型,则左边多出来的位用 0 来填充。
如果左操作数是有符号类型,则左边多出来的位用 符号位(最高位) 来填充。
向左移动 n个位置,就等同于除以 2的n次方

#include <iostream>
#include <Windows.h>
#include <bitset>
using namespace std;

int main()
{
    int num = 0xc3; // 1100 0011

    cout << (bitset<8>)num << " >> 3" << endl;
    cout << (bitset<8>)(num >> 3) << endl;

    system("pause");
    return 0;
}

image

七 应用

例:把一个整数的的最低4位设置为0110

#include <iostream>
#include <Windows.h>
#include <bitset>
using namespace std;

int main()
{
    int num = 0; 

    cout << "请输入一个整数:";
    cin >> num;
    cout << "十六进制:0x" << hex << num << endl;

    // 1 先将这个整数的最低4位清0
    num = num & (~0xf);
    // 2 把最低4位设置为0110
    num = num | 0x6;

    cout << "num的二进制" << (bitset<16>)num << endl;
    cout << "num的十六进制:" << hex << num << endl;

    system("pause");
    return 0;
}

image

posted @ 2022-04-07 03:19  荒年、  阅读(23)  评论(0编辑  收藏  举报