[复合运算符]
说明
复合按位异或运算符^=
通常与变量和常量一起使用,以切换(反转)变量中的特定位。
Bitwise XOR ^
运算符的回顾:
0 0 1 1 operand1 0 1 0 1 operand2 ---------- 0 1 1 0 (operand1 ^ operand2) - returned result
用法
x ^= y; // equivalent to x = x ^ y;
参数
x
: 多变的。允许的数据类型:char
,int
,long
.
y
: 变量或常量。允许的数据类型:char
,int
,long
.
示例代码
"bitwise XORed" 为 0 的位保持不变。所以如果 myByte 是一个字节变量,
myByte ^ 0b00000000 = myByte;
"bitwise XORed" 为 1 的位被切换,因此:
myByte ^ 0b11111111 = ~myByte;
注意事项和警告
因为我们在按位运算符中处理位 - 使用带有常量的二进制格式化程序很方便。这些数字在其他表示中仍然是相同的值,只是不太容易理解。此外,为清楚起见,显示了 0b00000000,但任何数字格式的零都是零。
因此 - 要切换变量的位 0 和 1,同时保持变量的其余部分不变,请使用复合按位异或运算符 (^=) 和常量 0b00000011
1 0 1 0 1 0 1 0 variable 0 0 0 0 0 0 1 1 mask ---------------------- 1 0 1 0 1 0 0 1
bits unchanged bits toggled
这是用符号 x 替换变量位的相同表示。 ~x 表示 x 的补码。
x x x x x x x x variable 0 0 0 0 0 0 1 1 mask ---------------------- x x x x x x ~x ~x
bits unchanged bits set
因此,如果:
myByte = 0b10101010;
myByte ^= 0b00000011 == 0b10101001;
相关用法
- Arduino ^用法及代码示例
- Arduino long用法及代码示例
- Arduino Arduino_EMBRYO_2 - setLengthXY()用法及代码示例
- Arduino ~用法及代码示例
- Arduino ArduinoBLE - bleDevice.advertisedServiceUuidCount()用法及代码示例
- Arduino const用法及代码示例
- Arduino Ethernet - server.begin()用法及代码示例
- Arduino ArduinoBLE - BLEService()用法及代码示例
- Arduino digitalWrite()用法及代码示例
- Arduino ArduinoBLE - bleCharacteristic.subscribe()用法及代码示例
- Arduino Servo - attach()用法及代码示例
- Arduino write()用法及代码示例
- Arduino Arduino_LSM9DS1 - readGyroscope()用法及代码示例
- Arduino ArduinoSound - FFTAnalyzer.input()用法及代码示例
- Arduino MKRGSM - gprs.attachGPRS()用法及代码示例
- Arduino WiFiNINA - WiFi.config()用法及代码示例
- Arduino MKRGSM - sms.read()用法及代码示例
- Arduino MKRNB - getCurrentCarrier()用法及代码示例
- Arduino Scheduler - Scheduler.startLoop()用法及代码示例
- Arduino Arduino_LSM9DS1 - magneticFieldAvailable()用法及代码示例
- Arduino MKRWAN - available()用法及代码示例
- Arduino ArduinoBLE - BLE.poll()用法及代码示例
- Arduino ArduinoBLE - bleCharacteristic.hasDescriptor()用法及代码示例
- Arduino Ethernet - EthernetUDP.parsePacket()用法及代码示例
- Arduino WiFi101 - WiFi.subnetMask()用法及代码示例
注:本文由纯净天空筛选整理自arduino.cc大神的英文原创作品 ^=。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。