Account
Categories

AND Operator (&)


Definition:

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

It takes two numbers (operands) and changes them into binary form.

And then, it matches bit by bit.

If both bits are 1, it gives 1 (true); otherwise, it gives 0 (false).

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

a = 12   → 1100
b = 15   → 1111

        1100
      & 1111
      --------
        1100

Result = 12

Syntax:

result = operand1 & operand2

Example:

a = 12   # binary: 1100
b = 15   # binary: 1111

result = a & b

print(result)

Output:

Explanation:

  1100 (12)
& 1111 (15)
------------
  1100 = 12

Exercise

Use Bitwise AND operator with values 10 and 7 and print the result using Python.