我想找...

搜尋

分享

目錄

|

[Bitwise Operators]

描述

The bitwise OR operator in C++ is the vertical bar symbol, |. Like the & operator, | operates independently each bit in its two surrounding integer expressions, but what it does is different (of course). The bitwise OR of two bits is 1 if either or both of the input bits is 1, otherwise it is 0. In other words:

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

範例程式碼

Here is an example of the bitwise OR used in a snippet of C++ code:

int a =  92;    // in binary: 00000000000000000000000001011100
int b = 101;    // in binary: 00000000000000000000000001100101
int c = a | b;  // result:    00000000000000000000000001111101, or 125 in decimal.

One of the most common uses of the Bitwise OR is to set multiple bits in a bit-packed number.

// 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;

參考


語法參考主頁面

86Duino 參考的文本是根據 Creative Commons Attribution-ShareAlike 3.0 License,部分文本是從 the Arduino reference 修改的。 參考中的代碼示例已發佈到公共領域。

發表評論

上部へスクロール