int a = 5; // 0101b (0x05)
int b = 12; // 1100b (0x0C)
int c = a | b; // 1101b (0x0D)
std::cout << "a = " << a << ", b = " << b << ", c = " << c << std::endl;
Output
a = 5, b = 12, c = 13
Why
A bit wise OR
operates on the bit level and uses the following Boolean truth table:
true OR true = true
true OR false = true
false OR false = false
When the binary value for a
(0101
) and the binary value for b
(1100
) are OR
'ed together we get the binary value of 1101
:
int a = 0 1 0 1
int b = 1 1 0 0 |
---------
int c = 1 1 0 1
The bit wise OR does not change the value of the original values unless specifically assigned to using the bit wise assignment compound operator |=
:
int a = 5; // 0101b (0x05)
a |= 12; // a = 0101b | 1101b