Account
Categories

OR Operator (|)


Definition:

It is a bitwise operator, called the Bitwise OR operator.

It operates on the binary representation of numbers.

It takes two numbers (operands) and converts them into binary form in which each corresponding bit is compared.

If one or both bits are 1, the result is 1 (True). If both bits are 0, the result is 0 (False).

Now you will clearly understand this with the help of the diagram.

a = 6   → 0110
b = 3   → 0011

        0110
      | 0011
      --------
        0111

Result = 7

Syntax:

result = operand1 | operand2

Example:

a = 6   # binary: 0110
b = 3   # binary: 0011

result = a | b

print(result)

Output:

Explanation:

  0110  (6)
| 0011  (3)
-------------
  0111  = 7

Exercise

Use Bitwise OR operator with values 5 and 2 and print the result using Python.