[複合運算符]
說明
複合按位 OR 運算符|=
通常與變量和常量一起使用,以 "set"(設置為 1)變量中的特定位。
Bitwise OR |
運算符的回顧:
0 0 1 1 operand1 0 1 0 1 operand2 ---------- 0 1 1 1 (operand1 | operand2) - returned result
用法
x |= y; // equivalent to x = x | y;
參數
x
: 多變的。允許的數據類型:char
,int
,long
.
y
: 變量或常量。允許的數據類型:char
,int
,long
.
示例代碼
"bitwise ORed" 為 0 的位不變,因此如果 myByte 是字節變量,
myByte | 0b00000000 = myByte;
"bitwise ORed" 為 1 的位設置為 1,因此:
myByte | 0b11111111 = 0b11111111;
注意事項和警告
因為我們在按位運算符中處理位 - 使用帶有常量的二進製格式化程序很方便。這些數字在其他表示中仍然是相同的值,隻是不太容易理解。此外,為清楚起見,顯示了 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 1 1
bits unchanged bits set
這是用符號 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 1 1
bits unchanged bits set
因此,如果:
myByte = 0b10101010;
myByte |= 0b00000011 == 0b10101011;
相關用法
- Arduino |用法及代碼示例
- 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.cc大神的英文原創作品 |=。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。