Rav*_*hur -11 c++ bit-manipulation bit-shift bitwise-operators bitwise-xor
有什么区别~i和INT_MAX^i
两者给出相同的没有.在二进制,但当我们打印否.输出不同,如下面的代码所示
#include <bits/stdc++.h>
using namespace std;
void binary(int x)
{
int i=30;
while(i>=0)
{
if(x&(1<<i))
cout<<'1';
else
cout<<'0';
i--;
}
cout<<endl;
}
int main() {
int i=31;
int j=INT_MAX;
int k=j^i;
int g=~i;
binary(j);
binary(i);
binary(k);
binary(g);
cout<<k<<endl<<g;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我把输出作为
1111111111111111111111111111111
0000000000000000000000000011111
1111111111111111111111111100000
1111111111111111111111111100000
2147483616
-32
Run Code Online (Sandbox Code Playgroud)
为什么k和g不同?