Bitwise operators

  • Bitwise operators are used to manipulating data at the bit level.
  • These operators are used for testing the bits or shifting them right or left. Bitwise operators may not be applied to float or double data types.
Name Meaning
& Bitwise Logical AND
| Bitwise Logical OR
^ Bitwise Logical XOR
<< Left shift
>> Right shift
~ One’s complement

Bitwise Logical AND ( & )

  • The result of bitwise Logical AND will be true only if both operands are true.
Result_exp1 Result_exp2 Final Result
0 0 0
0 1 0
1 0 0
1 1 1

Example:

n=10 & 6

Problem Solution Process

  • Convert both numbers 10 and 6 into binary.
  • Convert 0010 into decimal numbers.
Example

Membership in Operator

n=10
n=n & 6 
print(n)
Output
2

Bitwise Logical AND ( | )

  • The result of bitwise Logical OR will be true if anyone operand is true.
Result_exp1 Result_exp2 Final Result
0 0 0
0 1 1
1 0 1
1 1 1

Example:

n=10 | 6

Problem Solution Process

  • Convert both numbers 10 and 6 into binary.
  • Convert 1110 into decimal numbers.
Example

Membership in Operator

n=10
n=n | 6 
print(n)
Output
14

Bitwise XOR ( ^ )

  • The Bitwise XOR operator produce result 1 if both bits are different otherwise it will produce 0.

Example:

n=10 | 6

Problem Solution Process

  • Convert both numbers 10 and 6 into binary.
Example

Membership in Operator

n=10
n=n ^ 6 
print(n)
Output
1