Nearby lessons

34 of 159

Python - Bitwise Operators

📌 What You Will Learn
  • Define bitwise operators and how they work on binary values
  • Use the bitwise AND, OR, and XOR operators
  • Apply the bitwise complement operator with its formula
  • Use the left shift and right shift operators
  • Distinguish logical operators from bitwise operators

Introduction

Bitwise operators are used to perform operations at the bit level.

These operators work on binary values (0 and 1).

What are Bitwise Operators?

Before performing a bitwise operation, Python converts decimal numbers into binary form.

The operation is then performed bit by bit.

Bitwise Operators List

Operator Meaning
&Bitwise AND
|Bitwise OR
^Bitwise XOR
~Bitwise Complement
<<Left Shift
>>Right Shift

Understanding Binary Values

Consider the following example:

Decimal Binary
4100
5101

Example - Binary Values

🐍Code Cell
1a = 4
2b = 5
Output
Binary of 4 = 100
Binary of 5 = 101

1. Bitwise AND Operator (&)

The & operator returns 1 only when both bits are 1.

Otherwise, it returns 0.

Truth Table - AND Operator

Bit 1 Bit 2 Result
000
010
100
111

Example - Bitwise AND

🐍Code Cell
1print(4 & 5)
Output
4

Explanation

4 = 100
5 = 101

100
101
---
100

Binary 100 = Decimal 4
        

2. Bitwise OR Operator (|)

The | operator returns 1 if at least one bit is 1.

Truth Table - OR Operator

Bit 1 Bit 2 Result
000
011
101
111

Example - Bitwise OR

🐍Code Cell
1print(4 | 5)
Output
5

Explanation

4 = 100
5 = 101

100
101
---
101

Binary 101 = Decimal 5
        

3. Bitwise XOR Operator (^)

The ^ operator returns 1 only when both bits are different.

Truth Table - XOR Operator

Bit 1 Bit 2 Result
000
011
101
110

Example - Bitwise XOR

🐍Code Cell
1print(4 ^ 5)
Output
1

Explanation

4 = 100
5 = 101

100
101
---
001

Binary 001 = Decimal 1
        

4. Bitwise Complement Operator (~)

The ~ operator is also called the Bitwise NOT operator.

Formula:

~x = -(x + 1)

Example - Bitwise Complement

🐍Code Cell
1print(~4)
Output
-5

Explanation

~4

= -(4 + 1)

= -5
        

Another Example - Bitwise Complement

🐍Code Cell
1print(~10)
Output
-11

5. Left Shift Operator (<<)

The left shift operator shifts bits toward the left.

Formula:

x << n = x * 2ⁿ

Example - Left Shift

🐍Code Cell
1print(10 << 2)
Output
40

Explanation

10 * 2²

10 * 4

= 40
        

Binary Explanation - Left Shift

10 = 1010

After shifting 2 positions:

101000

Decimal = 40
        

6. Right Shift Operator (>>)

The right shift operator shifts bits toward the right.

Formula:

x >> n = x // 2ⁿ

Example - Right Shift

🐍Code Cell
1print(10 >> 2)
Output
2

Explanation

10 // 2²

10 // 4

= 2
        

Binary Explanation - Right Shift

10 = 1010

After shifting 2 positions:

10

Decimal = 2
        

Bitwise Operators with Variables

🐍Code Cell
1a = 4
2b = 5
3 
4print(a & b)
5print(a | b)
6print(a ^ b)
Output
4
5
1

Real World Usage of Bitwise Operators

Bitwise operators are commonly used in:

  • Networking
  • Cryptography
  • Embedded systems
  • Operating systems
  • Image processing

Example Areas

Operator Usage
&Masking
|Setting bits
^Toggling bits
<<Fast multiplication
>>Fast division

Difference Between Logical and Bitwise Operators

Logical Operators Bitwise Operators
Work on Boolean values. Work on binary bits.
Examples: and, or Examples: &, |
📝 Key Takeaways
  • Bitwise operators perform operations on binary values bit by bit
  • The & operator returns 1 only when both bits are 1
  • The | operator returns 1 when at least one bit is 1
  • The complement formula is ~x = -(x + 1)
  • Left shift multiplies by 2ⁿ and right shift divides by 2ⁿ
  • Bitwise operators are used in networking, cryptography, and image processing

🧠 Test Your Knowledge

8 Questions
Progress: 0 / 8