&

名称

&

説明

C++言語におけるビット単位のAND演算子は&であり、整数型を持つ二つの式に対して適用される。ビット単位のAND演算子は、式の個々のビットごとに演算が行われる。この規則に従うと、双方のビットが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演算が同時に起こる。

使用例

1
2
3
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ビットに対して、AND演算子が適用され、16個の演算の結果がcに格納される。上記の例の場合、2進数で010000100、10進数で68となる。

ビット単位のAND演算子のよくある使い方は、整数値から特定のビットを選択することであり、マスキングと呼ばれる。以下に例を示す(AVRアーキテクチャ専用)

1
PORTD = PORTD & 0b00000011;  // clear out bits 2 - 7, leave pins PD0 and PD1 untouched (xx & 11 == xx)

参照

言語 &&(論理積)

利用例 Bitmath Tutorial

オリジナルのページ

https://www.arduino.cc/reference/en/language/structure/bitwise-operators/bitwiseand/

Last Revision: 2020/12/26

最終更新日

January 4, 2024

inserted by FC2 system