入门_01_二进制和位运算

有符号的正数和负数二进制相互转换方式

1、将正数转换为负数:将正数二进制减一,然后按位取反

例子:

1的二进制表达式为0001-1是将1先减去1得到:0000,然后按位取反得到-1的二进制表达式1111

2的二进制表达式为0010-2是将2减去1得到:0001,然后按位取反得到-2的二进制表达式:1110

2、将负数转换为正数:观察符号位是否为1,如果是的话将二进制按位取反,然后加一,得到这个数的正数

例子:

-7的二进制表达式为1001,观察符号位为1,将表达式按位取反得到:0110,然后加上1,得到7的二进制表达式:0111

4位二进制可表达的数据范围是:-8 ~ -10 ~ 7

二进制和十六进制相互转换

二进制 -> 十六进制:每一个十六进制数对应4个二进制位

0b0000 -> 0x0000
0b0001 -> 0x0001
0b0010 -> 0x0010
0b0011 -> 0x0011
0b0100 -> 0x0100
0b0101 -> 0x0101
0b0110 -> 0x0110
0b0111 -> 0x0111
0b1000 -> 0x1000
0b1001 -> 0x1001
0b1010 -> 0xa
0b1011 -> 0xb
0b1100 -> 0xc
0b1101 -> 0xd
0b1110 -> 0xe
0b1111 -> 0xf

位运算和逻辑运算区别

位运算:|、&、^、~、<<、>>、>>>

例如:

1、 a | b

上述表达式会先将a,b都转换为2进制表达式,然后按位进行或运算,即只要有一个为1,那么这个位即为1

0010
0101
0111

2、 a & b

上述表达式会先将a,b都转换为2进制表达式,然后按位进行与运算,即只要有二者均为1,那么这个位才为1

0011
0101
0001

3、 a ^ b

上述表达式会先将a,b都转换为2进制表达式,然后按位进行异或运算,即只要有二者相同为0,不同为1

0011
0101
0110

4、 ~a

将a转换为二进制表达式,然后按位取反即可,如果需要得到准确的相反数,需要取反后+1,或者直接通过-a取的相反数

0110   a
1001   ~a
1010   ~a + 1

5、a << 1

将 a 转换为二进制表达式,然后向左移动1位,空位补零,最终的结果就是将a的值乘于2的移动位数的次方,例如向左移动1位,说明将结果乘为原来的2倍

0111     a
01110    a << 1

6、a >> 1

将 a 转换为二进制表达式,然后向右移动1位,空位补零,最终的结果就是将a的值除以2的移动位数的次方,例如向右移动1位,说明将结果除以原来的2倍

0111     a
0011     a >> 1

7、a >>> 1

在C++中,>>> 这个运算符并不存在,这是因为C++区分了有符号和无符号整数类型,并且为它们提供了不同的右位移运算符:

  • 对于有符号整数,当使用 >> 运算符时,结果的填充位将取决于编译器(大多数情况下是符号扩展,但这不是由标准所保证的)。
  • 对于无符号整数,>> 运算符总是执行逻辑右移,即用0填充最高位。
11010   a
11101   a >> 1
01101   static_cast<unsigned int>(a) >> 1

逻辑运算:||、&&

1、bool c = a || b

如果表达式a为true,那么不会执行表达式b

2、bool c = a&&b

如果表达式a为false,那么不会执行表达式b

加法运算逻辑

1、-3 + 5

将3转换为二进制,得到0011,然后将0011减去1,得到0010,按位取反得到1101
将5转换为二进制,得到0101
将两个数的二进制相加
   1101
   0101
(1)0010  
最高位的1溢出了,因此最终的结果为2

2、-3 + 2

将3转换为二进制,得到0011,然后将0011减去1,得到0010,按位取反得到1101
将5转换为二进制,得到0010
将两个数的二进制相加
   1101
   0010
   1111  
又要最高位为1,说明当前为负数,因此需要按位取反然后+1,得到最终值-1

代码实现

#include <bits/stdc++.h>
using namespace std;

class BinarySystem
{
public:
    // 打印一个int类型的数字的32位二进制表示
    static void printBinary(int num)
    {
        for (int i = 31; i >= 0; --i)
        {
            cout << ((num & (1 << i)) == 0 ? '0' : '1');
        }
        cout << endl;
    }

    static bool returnTrue()
    {
        cout << "进入了returnTrue函数" << endl;
        return true;
    }

    static bool returnFalse()
    {
        cout << "进入了returnFalse函数" << endl;
        return false;
    }
};

int main()
{
    // 非负数
    int a = 78;
    cout << a << endl;
    BinarySystem::printBinary(a);
    cout << "===a===" << endl;

    // 负数
    int b = -6;
    cout << b << endl;
    BinarySystem::printBinary(b);
    cout << "===b===" << endl;

    // 直接写二进制的形式定义变量
    int c = 0b1001110;
    cout << c << endl;
    BinarySystem::printBinary(c);
    cout << "===c===" << endl;

    // 直接写十六进制的形式定义变量
    int d = 0x4e;
    cout << d << endl;
    BinarySystem::printBinary(d);
    cout << "===d===" << endl;

    // ~、相反数
    cout << a << endl;
    BinarySystem::printBinary(a);
    BinarySystem::printBinary(~a);
    int e1 = ~a + 1;
    cout << e1 << endl;
    BinarySystem::printBinary(e1);
    int e2 = -a;
    cout << e2 << endl;
    BinarySystem::printBinary(e1);
    cout << "===e===" << endl;

    // int、long的最小值,取相反数、绝对值,都是自己
    int f = INT_MIN;
    cout << f << endl;
    BinarySystem::printBinary(f);
    cout << -f << endl; // 注意,这里可能不会显示预期的结果,因为INT_MIN的相反数溢出
    BinarySystem::printBinary(-f);
    cout << (~f + 1) << endl;
    BinarySystem::printBinary(~f + 1);
    cout << "===f===" << endl;

    // | & ^
    int g = 0b0001010;
    int h = 0b0001100;
    BinarySystem::printBinary(g | h);
    BinarySystem::printBinary(g & h);
    BinarySystem::printBinary(g ^ h);
    cout << "===g、h===" << endl;

    // << 位移操作
    int i = 0b0011010;
    BinarySystem::printBinary(i);
    BinarySystem::printBinary(i << 1);
    BinarySystem::printBinary(i << 2);
    BinarySystem::printBinary(i << 3);
    cout << "===i << ===" << endl;

    // 非负数 >> >>>,效果一样
    BinarySystem::printBinary(i);
    BinarySystem::printBinary(i >> 2);
    BinarySystem::printBinary(static_cast<unsigned int>(i) >> 2); // 使用无符号右移
    cout << "===i >> >>>===" << endl;

    // 负数 >> >>>,效果不一样
    int j = 0b11110000000000000000000000000000;
    BinarySystem::printBinary(j);
    BinarySystem::printBinary(j >> 2);
    BinarySystem::printBinary(static_cast<unsigned int>(j) >> 2); // 使用无符号右移
    cout << "===j >> >>>===" << endl;

    // 非负数左右位移
    int k = 10;
    cout << k << endl;
    cout << (k << 1) << endl;
    cout << (k << 2) << endl;
    cout << (k << 3) << endl;
    cout << (k >> 1) << endl;
    cout << (k >> 2) << endl;
    cout << (k >> 3) << endl;
    cout << "===k===" << endl;

    // 位操作与逻辑操作符的行为演示
    cout << "test1测试开始" << endl;
    bool test1 = BinarySystem::returnTrue() | BinarySystem::returnFalse();
    cout << "test1结果," << test1 << endl;
    cout << "test2测试开始" << endl;
    bool test2 = BinarySystem::returnTrue() || BinarySystem::returnFalse();
    cout << "test2结果," << test2 << endl;
    cout << "test3测试开始" << endl;
    bool test3 = BinarySystem::returnFalse() & BinarySystem::returnTrue();
    cout << "test3结果," << test3 << endl;
    cout << "test4测试开始" << endl;
    bool test4 = BinarySystem::returnFalse() && BinarySystem::returnTrue();
    cout << "test4结果," << test4 << endl;
    cout << "===|、&、||、&&===" << endl;

    return 0;
}
  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值