Part of the series

Several example codes

~1 min read · Updated Oct 1, 2025

Program Overview

This Python program reads an integer and a bit index n from the user.
It toggles the nth bit of the number — flipping it from 0 to 1 or from 1 to 0.
Bit manipulation is done using the XOR operator with a bitmask.


Python Code:


# Read number and bit index from user
num = int(input("Enter an integer: "))
n = int(input("Enter the bit index to toggle: "))

# Validate bit index
if n < 0:
    print("Bit index cannot be negative.")
else:
    # Toggle the n-th bit using XOR
    toggled = num ^ (1 << n)

    # Display result
    print("\n--- Result ---")
    print(f"Original number: {num} → Binary: {bin(num)}")
    print(f"After toggling bit {n}: {toggled} → Binary: {bin(toggled)}")

Sample Output:


Enter an integer: 55  
Enter the bit index to toggle: 5  

--- Result ---
Original number: 55 → Binary: 0b110111  
After toggling bit 5: 23 → Binary: 0b010111

Step-by-Step Explanation:

- The user inputs an integer and a bit index
- A bitmask (1 << n) is created to target the desired bit
- The XOR operator flips the bit at position n
- The result is printed in both decimal and binary formats


Written & researched by Dr. Shahin Siami

Related Articles

Complete Guide to Python Decorators – Enhancing Function Behavior with Reusable Logic

Decorators in Python are a powerful tool for modifying or extending the behavior of functions and classes without changing their original code. They allow developers to implement reusable logic such as logging, authentication, caching, or timing in a clean and maintainable way. This article explores the structure, definition, common use cases, and practical examples of decorators in Python.

Continue

Comprehensive Guide to Python Classes – Object-Oriented Design, Structure, Inheritance, and Practical Use

Classes in Python are the foundation of object-oriented programming, allowing developers to define complex data structures, encapsulate logic, and create scalable, maintainable systems. This article walks through the fundamentals of defining classes, constructors, methods, inheritance, encapsulation, and real-world applications in Python projects.

Continue

Several example codes

understand the Python programming language

Continue

Complete Guide to Getting Text Input from Users in Python

In Python, accepting input from users is one of the simplest yet most powerful features for interactive programming. This article explores the input() function in depth, explains how to cast types, validate user data, and use input within loops and conditions. Real-life examples help clarify each concept, making this guide perfect for beginners and intermediate learners.

Continue

Comprehensive Guide to Functions in Python: Structure, Types, and Use Cases

Functions in Python are essential building blocks for writing modular, reusable, and maintainable code. This article introduces function definitions, distinguishes between built-in and user-defined functions, explores parameters and return values, and presents advanced topics such as recursion and lambda expressions. Whether you're a beginner or brushing up for interviews, this guide offers a clear and accessible foundation.

Continue

Loops, Iterators, and Repetition Control in Python

Loops, Iterators, and Repetition Control in Python

Continue