|

名称

|

説明

C++言語におけるビット単位のOR演算子は|である。ビット単位の&演算子同様、ビット単位のOR演算子は、整数型を持つ二つの式の個々のビットごとに演算(もちろん異なる演算)が行われる。どちらかのビットが1のとき結果が1となり、そうでなければ0である。例を以下に示す。

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

使用例

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

ビット単位のOR演算子の共通した役割は、ビット列の複数のビットを1にセットすることである。

1
2
3
4
// 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;

参照

言語 ||(論理和)

利用例 Bitmath Tutorial

オリジナルのページ

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

Last Revision: 2020/12/26

最終更新日

January 4, 2024

inserted by FC2 system