Python Tutorials · Python Operators

Bitwise Operators

Learn all about Bitwise Operators in this comprehensive tutorial.

5 min read beginner
  • Bitwise operators are used to compare (binary) numbers:

Bitwise Operators

Bitwise operators are used to compare (binary) numbers:

OperatorNameDescriptionExampleTry it
&ANDSets each bit to 1 if both bits are 1x & yTry it »
|ORSets each bit to 1 if one of two bits is 1x | yTry it »
^XORSets each bit to 1 if only one of two bits is 1x ^ yTry it »
~NOTInverts all the bits~xTry it »
<<Zero fill left shiftShift left by pushing zeros in from the right and let the leftmost bits fall offx << 2Try it »
>>Signed right shiftShift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall offx >> 2Try it »

Examples

python
python
python

Module quiz

2 questions
1

Which of the following is true about Bitwise Operators?

2

What is the most common pitfall when working with Bitwise Operators?

Answer all questions to submit.