Bitwise Operations
What Are Bitwise Operations?¶
Bitwise operations work directly on the binary representations of numbers (or other data). Instead of treating numbers as numerical values, we manipulate their individual bits (0s and 1s). These operations are fast, low-level, and super useful in scenarios like:
- Optimizing code for performance.
- Working with hardware, networking, or cryptography.
- Manipulating flags, permissions, or pixel data in images.
Python provides six main bitwise operators: AND, OR, XOR, NOT, Left Shift, and Right Shift. Let’s learn each one with examples.
Step 1: Understanding Binary Numbers¶
Before we jump into the operators, you need to know how numbers are represented in binary. For example:
- The number
5
in binary is0101
. - The number
3
in binary is0011
.
Each digit in a binary number is a bit. The rightmost bit is the least significant bit (LSB), and the leftmost is the most significant bit (MSB).
You can convert integers to binary in Python using the bin()
function:
Step 2: The Bitwise Operators¶
Let’s explore each bitwise operator, what it does, and how to use it in Python.
1. Bitwise AND (&
)¶
- What it does: Compares each bit of two numbers. If both bits are
1
, the result is1
; otherwise, it’s0
. - Use case: Masking (selecting specific bits) or checking if certain bits are set.
Example:
Python | |
---|---|
How it works:
2. Bitwise OR (|
)¶
- What it does: Compares each bit of two numbers. If at least one bit is
1
, the result is1
; otherwise, it’s0
. - Use case: Setting specific bits or combining flags.
Example:
Python | |
---|---|
How it works:
3. Bitwise XOR (^
)¶
- What it does: Compares each bit of two numbers. If the bits are different (one is
1
, the other is0
), the result is1
; otherwise, it’s0
. - Use case: Toggling bits, encryption, or finding unique elements.
Example:
Python | |
---|---|
How it works:
Fun Fact: XORing a number with itself gives 0
, and XORing a number with 0
gives the number itself.
4. Bitwise NOT (~
)¶
- What it does: Flips all the bits of a number (
0
becomes1
,1
becomes0
). In Python, this is equivalent to-(x + 1)
due to how negative numbers are represented (two’s complement). - Use case: Inverting bits or computing complements.
Example:
Python | |
---|---|
Explanation:
- For a number x
, ~x = -(x + 1)
.
- So, ~5 = -(5 + 1) = -6
.
5. Left Shift (<<
)¶
- What it does: Shifts all bits of a number to the left by a specified number of positions. Zeros are filled in from the right. This is equivalent to multiplying by
2^n
(wheren
is the shift amount). - Use case: Fast multiplication or aligning bits.
Example:
Python | |
---|---|
How it works:
6. Right Shift (>>
)¶
- What it does: Shifts all bits of a number to the right by a specified number of positions. For positive numbers, zeros are filled in from the left. This is equivalent to dividing by
2^n
(integer division). - Use case: Fast division or extracting specific bits.
Example:
How it works:
Step 3: Practical Examples¶
Let’s apply bitwise operations to solve some real-world problems.
Example 1: Checking if a Number is Even or Odd¶
- A number is even if its least significant bit (LSB) is
0
, and odd if it’s1
. - We can use
&
with1
to check the LSB.
Python | |
---|---|
Example 2: Swapping Two Numbers Without a Temporary Variable¶
- XOR can be used to swap values efficiently.
Python | |
---|---|
Example 3: Setting and Clearing Bits¶
- Use
|
to set a bit (turn it to1
) and&
with~
to clear a bit (turn it to0
).
Example 4: Counting Set Bits (Hamming Weight)¶
- Count the number of
1
bits in a number’s binary representation.