0% found this document useful (0 votes)
3 views13 pages

Bitwise Operators in Python

The document provides an overview of bitwise operators in Python, including their definitions, functions, and practical applications. It covers operators such as AND, OR, XOR, NOT, and shift operations, along with examples and truth tables. Additionally, it highlights the usefulness of bitwise operations in areas like fast calculations, flags, graphics, and cryptography.

Uploaded by

kavya.gupta.ug25
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views13 pages

Bitwise Operators in Python

The document provides an overview of bitwise operators in Python, including their definitions, functions, and practical applications. It covers operators such as AND, OR, XOR, NOT, and shift operations, along with examples and truth tables. Additionally, it highlights the usefulness of bitwise operations in areas like fast calculations, flags, graphics, and cryptography.

Uploaded by

kavya.gupta.ug25
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Bitwise Operators in Python

Understanding Binary Operations

Bitwise Operators 1 / 13
Outline

1 Introduction

2 Bitwise Operators

3 Shift Operators

4 Summary

Bitwise Operators 2 / 13
What are Bitwise Operators?

Definition
Bitwise operators work on binary representations of numbers, performing
operations at the bit level.

Binary Basics:
Decimal 5 = Binary 101
Decimal 3 = Binary 011
Operations work on individual bits (0s and 1s)

Bitwise Operators 3 / 13
Bitwise AND Operator (&)

Returns 1 if BOTH bits are 1, otherwise returns 0

Truth Table: Python Example:


a = 5 # Binary : 101
A B A&B b = 3 # Binary : 011
0 0 0
0 1 0 result = a & b
print ( result ) # Output : 1
1 0 0
1 1 1 # Binary operation :
# 101 (5)
# & 011 (3)
# ------ -
# 001 (1)

Bitwise Operators 4 / 13
Bitwise OR Operator (—)

Returns 1 if AT LEAST ONE bit is 1, otherwise returns 0

Truth Table: Python Example:


a = 5 # Binary : 101
A B A—B b = 3 # Binary : 011
0 0 0
0 1 1 result = a | b
print ( result ) # Output : 7
1 0 1
1 1 1 # Binary operation :
# 101 (5)
# | 011 (3)
# ------ -
# 111 (7)

Bitwise Operators 5 / 13
Bitwise XOR Operator (ˆ)

Returns 1 if bits are DIFFERENT, 0 if they are SAME

Truth Table: Python Example:


a = 5 # Binary : 101
A B AˆB b = 3 # Binary : 011
0 0 0
0 1 1 result = a ^ b
print ( result ) # Output : 6
1 0 1
1 1 0 # Binary operation :
# 101 (5)
# ^ 011 (3)
# ------ -
# 110 (6)

Bitwise Operators 6 / 13
Bitwise NOT Operator (˜)

Inverts all bits: 0 becomes 1, and 1 becomes 0

Important: Python Example:


Python uses two’s a = 5
complement result = ~ a
Formula: ˜x = -(x + 1) print ( result ) # Output : -6

# Explanation :
Examples: # ~5 = -(5 + 1) = -6

˜5 = -6 # More examples :
print (~10) # -11
˜10 = -11 print (~0) # -1
˜0 = -1 print (~( -1) ) # 0

Bitwise Operators 7 / 13
Left Shift Operator (¡¡)

Shifts bits to the LEFT, filling with 0s on the right

Effect: Python Example:


Multiplies by 2 for each shift a = 5 # Binary : 101

Formula: x ¡¡ n = x × 2n result = a << 1


print ( result ) # Output : 10

Visual: # More examples :


print (5 << 1) # 10
print (5 << 2) # 20
101 (5) print (3 << 3) # 24
↓ shift left 1
1010 (10) # 5 << 2 = 5 * (2^2) = 20

Bitwise Operators 8 / 13
Right Shift Operator (¿¿)

Shifts bits to the RIGHT, discarding bits on the right

Effect: Python Example:


Divides by 2 for each shift a = 10 # Binary : 1010

Rounds down result = a >> 1


print ( result ) # Output : 5
Formula: x ¿¿ n = x // 2n
# More examples :
print (10 >> 1) # 5
Visual: print (10 >> 2) # 2
print (20 >> 1) # 10
1010 (10)
# 10 >> 2 = 10 // (2^2) = 2
↓ shift right 1
101 (5)

Bitwise Operators 9 / 13
Quick Reference: All Bitwise Operators

Operator Symbol Description Example


AND & 1 if both are 1 5 & 3 = 1
OR | 1 if any is 1 5 | 3 = 7
XOR ^ 1 if different 5 ^ 3 = 6
NOT ~ Inverts bits ~5 = -6
Left Shift << Multiply by 2n 5 << 1 = 10
Right Shift >> Divide by 2n 10 >> 1 = 5

Bitwise Operators 10 / 13
Practical Applications

Where bitwise operators are useful:

1 Fast Calculations
Shift operations are faster than multiplication/division

2 Flags & Permissions


Store multiple boolean values in one integer

3 Graphics & Gaming


Manipulate colors, masks, and pixel data

4 Cryptography & Security


Essential for encryption algorithms

Bitwise Operators 11 / 13
Practice Example: Swap Two Numbers

Using XOR operator to swap without a temporary variable

# Swap two numbers using XOR

a = 5
b = 9

print ( f " Before : a = { a } , b = { b } " )

# XOR swap trick


a = a ^ b
b = a ^ b
a = a ^ b

print ( f " After : a = { a } , b = { b } " )

# Output :
# Before : a = 5 , b = 9
# After : a = 9 , b = 5

Bitwise Operators 12 / 13
Key Takeaways

Bitwise operators work on binary representations

They are fast and memory-efficient

Useful for optimization and low-level programming

Practice with simple examples to master them!

Thank You!

Bitwise Operators 13 / 13

You might also like