| - bitwise OR

suggest change
// :glot
#include <iostream>

int main() {
    const int a = 5;     // 0101b  (0x05)
    const int b = 12;    // 1100b  (0x0C)
    const int c = a | b; // 1101b  (0x0D)
    
    std::cout << "a = " << a << ", b = " << b << ", c = " << c << '\n';
}
a = 5, b = 12, c = 13

Why

A bitwise 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 bitwise OR does not change the value of the original values unless specifically assigned to using the bitwise assignment compound operator |=:

// :glot
#include <iostream>

int main() {
    int a = 5;  // 0101b  (0x05)
    a |= 12;    // a = 0101b | 1100b == 1101b (13)

    std::cout << "a = " << a << '\n';
}
a = 13

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents