🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpython

python Documentation

LOADING ENGINE...

Bitwise Operators

AI & DATA SCIENCE // bitwise-operators

Bitwise operators (&, |, ^, ~, <<, >>) manipulate integers at the level of their individual binary bits.

Syntax

a & b
a | b
a ^ b
~a
a << n
a >> n

Deep Dive Course

Bitwise operators treat integers as sequences of binary bits rather than as whole numeric values: & (AND) and | (OR) combine bits position by position, ^ (XOR) sets a bit only when its operands differ, ~ flips every bit, and << / >> shift bits left or right, which for positive integers is equivalent to multiplying or dividing by a power of two. They're most common in low-level code — flags, masks, binary protocols — rather than everyday application logic.

1Understanding Bitwise Operators

Bitwise operators treat integers as sequences of binary bits rather than as whole numeric values: & (AND) and | (OR) combine bits position by position, ^ (XOR) sets a bit only when its operands differ, ~ flips every bit, and << / >> shift bits left or right, which for positive integers is equivalent to multiplying or dividing by a power of two. They're most common in low-level code — flags, masks, binary protocols — rather than everyday application logic.

💡

Left-shifting a positive integer is a fast, idiomatic way to multiply it by a power of two, and it's often used to define bit-flag constants readably, so each flag occupies its own distinct bit.

editor.html
print(5 & 3)
print(5 | 2)
print(5 ^ 1)
print(~5)
localhost:3000

2Practical Example

Here is a real-world application of Bitwise Operators showing how it is used in production Python code.

editor.html
READ = 1 << 0
WRITE = 1 << 1
EXECUTE = 1 << 2
permissions = READ | WRITE
print(permissions)
print(bool(permissions & WRITE))
localhost:3000

3Best Practices

Follow these guidelines when working with Bitwise Operators:

1. Use bitwise operators for flags, masks, and binary protocol parsing, not as a substitute for the logical and/or operators, which work on truthiness rather than bits

2. Prefer named constants defined with shifts over magic numbers when defining a set of flag values

3. Reach for a number's built-in bit-length method or the bin() function instead of manual bit-shifting loops when you just need to inspect its binary representation

⚠️

Tip: Left-shifting a positive integer is a fast, idiomatic way to multiply it by a power of two, and it's often used to define bit-flag constants readably, so each flag occupies its own distinct bit.

editor.html
print(5 & 3)
print(5 | 2)
print(5 ^ 1)
print(~5)
localhost:3000

Examples

Example 01Basic Usage
print(5 & 3)
print(5 | 2)
print(5 ^ 1)
print(~5)
Example 02Advanced Example
READ = 1 << 0
WRITE = 1 << 1
EXECUTE = 1 << 2
permissions = READ | WRITE
print(permissions)
print(bool(permissions & WRITE))

Best Practices

  • Use bitwise operators for flags, masks, and binary protocol parsing, not as a substitute for the logical and/or operators, which work on truthiness rather than bits
  • Prefer named constants defined with shifts over magic numbers when defining a set of flag values
  • Reach for a number's built-in bit-length method or the bin() function instead of manual bit-shifting loops when you just need to inspect its binary representation

Interview Question

How are bit-flag constants like READ, WRITE, and EXECUTE typically defined and combined using bitwise operators?

Hint: Think about giving each flag its own distinct bit.

Each flag is defined as a power of two, so it occupies exactly one distinct bit, commonly written with a left shift for readability, like shifting 1 left by increasing amounts for each successive flag. Combining flags with | sets multiple distinct bits into one integer, and checking whether a specific flag is present uses & against that flag's bit, then testing whether the result is nonzero.

Exercises

MediumPractice using Bitwise Operators in a real scenario.
View Solution
print(5 & 3)
print(5 | 2)
print(5 ^ 1)
print(~5)

Frequently Asked Questions

How are bit-flag constants like READ, WRITE, and EXECUTE typically defined and combined using bitwise operators?

Each flag is defined as a power of two, so it occupies exactly one distinct bit, commonly written with a left shift for readability, like shifting 1 left by increasing amounts for each successive flag. Combining flags with | sets multiple distinct bits into one integer, and checking whether a specific flag is present uses & against that flag's bit, then testing whether the result is nonzero.

Related Functions

arithmetic-operatorsbytesint()