intermediatePython
What are bitwise operators?
Answer
Clear, interview-ready explanation
Bitwise operators perform operations on the binary representations of integers.
| Operator | Name | Description |
|---|---|---|
&
|
Bitwise AND |
Sets a bit when both bits are 1
|
| ` | ` | Bitwise OR |
^
|
Bitwise XOR | Sets a bit when the bits are different |
~
|
Bitwise NOT | Inverts all bits |
<<
|
Left shift | Shifts bits to the left |
>>
|
Right shift | Shifts bits to the right |
Example using bitwise AND:
print(5 & 3)
Binary calculation:
5 = 101
3 = 011
---------
001 = 1
Bitwise operators are commonly used with permissions, flags, binary data, networking, and low-level programming.