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:
| Operator | Name | Description | Example | Try it |
|---|---|---|---|---|
| & | AND | Sets each bit to 1 if both bits are 1 | x & y | Try it » |
| | | OR | Sets each bit to 1 if one of two bits is 1 | x | y | Try it » |
| ^ | XOR | Sets each bit to 1 if only one of two bits is 1 | x ^ y | Try it » |
| ~ | NOT | Inverts all the bits | ~x | Try it » |
| << | Zero fill left shift | Shift left by pushing zeros in from the right and let the leftmost bits fall off | x << 2 | Try it » |
| >> | Signed right shift | Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off | x >> 2 | Try it » |
Examples
python
python
python
Module quiz
2 questions1
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.