[複合運算符]
說明
複合位與運算符&=
通常與變量和常量一起使用,以強製變量中的特定位為低狀態(為 0)。這在編程指南中通常稱為"clearing" 或"resetting" 位。
Bitwise AND &
運算符的回顧:
0 0 1 1 operand1 0 1 0 1 operand2 ---------- 0 0 0 1 (operand1 & operand2) - returned result
用法
x &= y; // equivalent to x = x & y;
參數
x
: 多變的。允許的數據類型:char
,int
,long
.
y
: 變量或常量。允許的數據類型:char
,int
,long
.
示例代碼
"bitwise ANDed" 為 0 的位被清除為 0,因此,如果 myByte 是字節變量,
myByte & 0b00000000 = 0;
"bitwise ANDed" 為 1 的位保持不變,因此,
myByte & 0b11111111 = myByte;
注意事項和警告
因為我們在按位運算符中處理位 - 使用帶有常量的二進製格式化程序很方便。這些數字在其他表示中仍然是相同的值,隻是不太容易理解。此外,為清楚起見,顯示了 0b00000000,但任何數字格式的零都是零(嗯,那裏有什麽哲學嗎?)
因此 - 要清除(設置為零)變量的位 0 和 1,同時保持變量的其餘部分不變,請使用複合位與運算符 (&=) 和常量 0b11111100
1 0 1 0 1 0 1 0 variable 1 1 1 1 1 1 0 0 mask ---------------------- 1 0 1 0 1 0 0 0
bits unchanged bits cleared
這是用符號 x 替換變量位的相同表示
x x x x x x x x variable 1 1 1 1 1 1 0 0 mask ---------------------- x x x x x x 0 0
bits unchanged bits cleared
因此,如果:
myByte = 0b10101010;
myByte &= 0b11111100; // results in 0b10101000
相關用法
- 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大神的英文原創作品 &=。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。