Bitwise Operators

Python · Operators

Theory

Bitwise operators work on the binary digits (bits) of integers. With a = 5 (binary 101) and b = 3 (binary 011): & keeps bits set in both (001 = 1), | keeps bits set in either (111 = 7), ^ keeps bits set in exactly one (110 = 6), << shifts bits left (5 << 1 is 1010 = 10, the same as multiplying by 2), and >> shifts right (5 >> 1 is 2, like floor dividing by 2). bin(5) shows '0b101'.

~ inverts all bits; because Python integers behave like two's complement with infinite sign bits, ~x always equals -x - 1, so ~5 is -6. Do not confuse & and | with and and or: the bitwise versions work bit by bit and do not short-circuit. In data work you meet them most in pandas, where & and | combine boolean columns, e.g. (df.age > 18) & (df.city == 'Pune'), and each condition needs its own parentheses because & binds tighter than >.

Why we use it

Bits are a compact way to store many yes/no flags in one integer, such as permission masks, and shifts are fast multiply/divide by powers of two. In pandas, & and | are the only way to combine filter conditions.

When to use it

Reach for bitwise operators for flags, masks, low-level protocols and pandas boolean filters. For ordinary if conditions on Python values, use and, or and not instead, which are clearer and short-circuit.

How it works

  1. Evaluate 5 & 3
  2. Write both numbers in binary: 101 and 011
  3. Compare bit by bit at each position
  4. Is the bit 1 in both numbers? Yes: Result bit is 1. No: Result bit is 0.
  5. Convert the result bits back to decimal: 001
  6. Output: 1

Syntax

&  |  ^  ~  <<  >>

Example

print(5 & 3)   # 1
print(5 | 3)   # 7
print(5 << 1)  # 10

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic