Bitwise Or

The bitwise Or operation, signified by the | operator, takes two values (normally integers) as operands and returns a value of the same type. It performs an or operation on corresponding bits of the two operands. If a particular bit is on in either of the two operands, the corresponding bit in the result is on, otherwise it is off. Here is an example

Operand 1  00000000 00000000 00000000 00110101
Operand 2  00000000 00000000 00000000 00011000
           -------- -------- -------- --------
Result     00000000 00000000 00000000 00111101
This is often used to set flags for a function. For example, when a file is opened, there are a number of different flags which can be set. Typically each flag is given a corresponding symbolic name and refers to a single bit. This is done in a header file.

For example, a header file might contain code like this.

#define O_RDONLY        0x0   /*00000000 00000000 00000000 00000000*/
#define O_WRONLY        0x1   /*00000000 00000000 00000000 00000001*/
#define O_RDWR          0x2   /*00000000 00000000 00000000 00000010*/
#deinfe O_NDELAY        0x4   /*00000000 00000000 00000000 00000100*/
#define O_APPEND        0x8   /*00000000 00000000 00000000 00001000*/
#define O_CREAT         0x10  /*00000000 00000000 00000000 00010000*/
#define O_TRUNC         0x20  /*00000000 00000000 00000000 00100000*/
#define O_EXCL          0x40  /*00000000 00000000 00000000 01000000*/
#define O_NOCTTY        0x80  /*00000000 00000000 00000000 10000000*/
Each of these sets a single bit of a 32 bit word on. Thus, if open is called like this:

fd = open("myfile",O_WRONLY | O_APPEND | O_CREAT)

The second argument will have three bits set. It would look like this in binary

00000000 00000000 00000000 00011001