Bitwise Operators in Python

Python Bitwise operators act on bits and perform bit-by-bit operations. These are used to operate on binary numbers.

OperatorDescriptionSyntax
&Bitwise ANDx & y
|Bitwise ORx | y
~Bitwise NOT~x
^Bitwise XORx ^ y
>>Bitwise right shiftx>>
<<Bitwise left shiftx<<

Precedence of Bitwise Operators in Python

The precedence of Bitwise Operators in Python is as follows:

  1. Bitwise NOT
  2. Bitwise Shift
  3. Bitwise AND
  4. Bitwise XOR
  5. Bitwise OR

Bitwise Operators in Python

Here is an example showing how Bitwise Operators in Python work:

Example: The code demonstrates various bitwise operations with the values of ‘a’ and ‘b’. It performs bitwise AND (&), OR (|), NOT (~), XOR (^), right shift (>>), and left shift (<<) operations and prints the results. These operations manipulate the binary representations of the numbers.

Python

a = 10
b = 4
print(a & b)
print(a | b)
print(~a)
print(a ^ b)
print(a >> 2)
print(a << 2)


Output
0
14
-11
14
2
40



Python Operators

In Python programming, Operators in general are used to perform operations on values and variables. These are standard symbols used for logical and arithmetic operations. In this article, we will look into different types of Python operators. 

  • OPERATORS: These are the special symbols. Eg- + , * , /, etc.
  • OPERAND: It is the value on which the operator is applied.

Similar Reads

Types of Operators in Python

Arithmetic OperatorsComparison OperatorsLogical OperatorsBitwise OperatorsAssignment OperatorsIdentity Operators and Membership Operators...

Arithmetic Operators in Python

Python Arithmetic operators are used to perform basic mathematical operations like addition, subtraction, multiplication, and division....

Example of Arithmetic Operators in Python

Division Operators...

Comparison of Python Operators

In Python Comparison of Relational operators compares the values. It either returns True or False according to the condition....

Logical Operators in Python

Python Logical operators perform Logical AND, Logical OR, and Logical NOT operations. It is used to combine conditional statements....

Bitwise Operators in Python

Python Bitwise operators act on bits and perform bit-by-bit operations. These are used to operate on binary numbers....

Assignment Operators in Python

Python Assignment operators are used to assign values to the variables....

Identity Operators in Python

In Python, is and is not are the identity operators both are used to check if two values are located on the same part of the memory. Two variables that are equal do not imply that they are identical....

Membership Operators in Python

In Python, in and not in are the membership operators that are used to test whether a value or variable is in a sequence....

Ternary Operator in Python

in Python, Ternary operators also known as conditional expressions are operators that evaluate something based on a condition being true or false. It was added to Python in version 2.5....

Precedence and Associativity of Operators in Python

In Python, Operator precedence and associativity determine the priorities of the operator....

Python Operator Exercise Questions

Below are two Exercise Questions on Python Operators. We have covered arithmetic operators and comparison operators in these exercise questions. For more exercises on Python Operators visit the page mentioned below....

Contact Us