C 复合赋值运算符 ^= 平均值

C compound assignment operator ^= mean

本文关键字:平均值 赋值运算符 复合      更新时间:2023-10-16

我不明白这段代码中使用的^运算符。请帮助我。

#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
for(int i = 0; i < n; ++i) {
cin >> a[i];
}
for(int i=0;i<n;i++){
int ans = a[i]^1;
cout<<" ans "<<ans<<"  ";
}
return 0;
}

它是XOR运算符(独占或操作(:
将切换(反转(操作数的位,其中任何位在另一个操作数中具有值为1

的位置
int ans = a[i]^1;

例如,一个 4 字节大小,在 i:

a[i] = 00000000 0000000 1000000 1000001
1  = 00000000 0000000 0000000 0000001 
---------------------------------- XOR
00000000 0000000 1000000 1000000
^ --> 1st bit of a[i] get toggled