[英]what is the value of the expression involving bitwise operation in C++
在我的機器上,以下表達式:-
int main()
{
int q = 0b01110001;
cout << q << endl;
cout << (~q << 6);
}
打印以下內容:-
113
-7296
我試過假設 16 位整數,但我的答案與按位運算后獲得的值不匹配。
這只是未定義行為的情況還是我在這里遺漏了什么?
您可以使用bitset
檢查整數的二進制表示。
程序 :
#include <iostream>
#include <bitset>
using namespace std;
int main() {
int q = 0b01110001;
cout << q << "\n";
cout << bitset<(sizeof(int) * 8)>(q) << "\n";
cout << ~q << "\n";
cout << bitset<(sizeof(int) * 8)>(~q) << "\n";
cout << (~q << 6) << "\n";
cout << bitset<(sizeof(int) * 8)>(~q << 6) << "\n";
}
輸出 :
113
00000000000000000000000001110001
-114
11111111111111111111111110001110
-7296
11111111111111111110001110000000
如您所見, ~
反轉所有位。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.