C++ 按位运算

一、cout如果输出按位运算结果,需要使用int强制转换,如果是printf,前面是%d就可以。其他地方代码直接用。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main()
{

    int a, b;
    cin>> a>> b;
    int cnt[10];
    memset(cnt, 0, sizeof(cnt));
    cnt[a|b]++;
    cout<< int(a|b)<< endl;
    cout<< cnt[3]<< endl;
    printf("%d\n", a|b);
    return 0;
}

二、按位与(&)

三、按位或(|)

四、按位异或(^)

五、按位取反(~)

六、左移(<<k):左移k位,左边丢弃,右边补零

七、右移(>>k):右移k维,正数左边补零,负数左边补1, 右边丢弃

八、无符号右移(>>>k):右移k维,左边补零, 右边丢弃

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章