| is the bitwise OR operator, where as || is a logical OR operator. Namely, the former is used to “combine” bits from two numerical values as a union, while the latter evaluates to true if either the condition on the left or right side of the operator is true.
In particular, bitwise operators ( not that should be confused with logical operators) work on each bit of numbers (in the same ordinal position) and accordingly calculate the result. In the case of bitwise OR resulting bit is 1 if the bit is 1, and 0 only if both bits are 0. For example, 1 | 2 = 3 because:
1 = 0001 2 = 0010 -------- 0011 = 3
In addition, 2 | 3 = 3 because:
2 = 0010 3 = 0011 -------- 0011 = 3
This may seem confusing at first, but in the end you will get it. A bitwise OR used in most cases to set flags in a bitfield. That is, a value containing an on / off state for a set of related conditions in a single value (usually a 32-bit number). In Win32, a window style value is a good example of a bit field, where each style is represented by a single bit (or flag), for example WS_CAPTION, which indicates whether the window has a title bar.
Javert93
source share