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維,左邊補零, 右邊丟棄

 

 

 

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