[位運算符]
說明
C++ 中的按位與運算符是一個與號 &
,用於兩個其他整數表達式之間。按位與對周圍表達式的每個位位置進行獨立運算,根據此規則:如果兩個輸入位均為 1,則結果輸出為 1,否則輸出為 0。
另一種表達方式是:
0 0 1 1 operand1 0 1 0 1 operand2 ---------- 0 0 0 1 (operand1 & operand2) - returned result
在 Arduino 中,int 類型是 16 位值,因此在兩個 int 表達式之間使用 & 會導致同時發生 16 次 AND 操作。
示例代碼
在如下代碼片段中:
int a = 92; // in binary: 0000000001011100
int b = 101; // in binary: 0000000001100101
int c = a & b; // result: 0000000001000100, or 68 in decimal.
a 和 b 中的 16 位中的每一個都通過按位與處理,所有 16 位結果都存儲在 c 中,產生二進製值 01000100,即十進製 68。
按位與最常見的用途之一是從整數值中選擇特定位(或位),通常稱為屏蔽。請參閱下麵的示例(特定於 AVR 架構)。
PORTD = PORTD & 0b00000011; // clear out bits 2 - 7, leave pins PD0 and PD1 untouched (xx & 11 == xx)
相關用法
- 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大神的英文原創作品 &。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。