| (bitwise or)

説明

C++のビットごとのOR演算子は、縦棒記号の|です。| は、& 演算子と同様に、周囲の 2 つの整数式の各ビットを独立して操作しますが、その内容は異なります。2つのビットごとのOR演算子は、入力ビットのいずれかまたは両方が1であれば1、そうでなければ0となります。

0  0  1  1    operand1
0  1  0  1    operand2
----------
0  1  1  1    (operand1 | operand2) - returned result

プログラム例

int a =  92;    // in binary: 0000000001011100
int b = 101;    // in binary: 0000000001100101
int c = a | b;  // result:    0000000001111101, or 125 in decimal.

ビットごとの ORの最も一般的な使い方は、ビット列の複数のビットを設定することです。

// Note: This code is AVR architecture specific
// set direction bits for pins 2 to 7, leave PD0 and PD1 untouched (xx | 00 == xx)
// same as pinMode(pin, OUTPUT) for pins 2 to 7 on Uno or Nano
DDRD = DDRD | 0b11111100;
タイトルとURLをコピーしました