Python Program to Toggle the Last m Bits

In the previous article, we have discussed Python Program to Count Minimum Bits to Flip such that XOR of A and B Equal to C

Given a number n, the task is to toggle the last m bits of the given number in its binary representation.

Toggling: 

A toggling operation changes the value of a bit from 0 to 1 and from 1 to 0.

let Given number =30 m=3

The binary representation of 30=11110

Binary representation after toggling the last 3 bits is =11001

Decimal equivalent after toggling =25

Examples:

Example1:

Input:

Given Number = 30
Given m value =  3

Output:

The given number{ 30 } after toggling the given last m { 3 } bits =  25

Example2:

Input:

Given Number = 45
Given m value =  2

Output:

The given number{ 45 } after toggling the given last m { 2 } bits =  46

Program to Toggle the Last m Bits in Python

Below are the ways to toggle the given last m bits of a given number in python:

Method #1: Using Xor(^) Operator (Static Input)

Approach:

  • Give the number as static input and store it in a variable.
  • Give the value of m as static input and store it in another variable.
  • Pass the given number, m value as the arguments to the toglng_lstmbits function.
  • Create a  function to say toglng_lstmbits which takes the given number, m value as the arguments and returns the number after toggling the given last m bits.
  • Apply the left shift operator to 1 and the above-given m value and subtract 1 from it.
  • Store it in another variable.
  • Return the XOR value of the given number and the above result.
  • Print the number after toggling the given last m bits.
  • The Exit of the Program.

Below is the implementation:

# Create a  function to say toglng_lstmbits which takes the given number, m value as the
# arguments and returns the number after toggling the given last m bits.


def toglng_lstmbits(gvn_numb, m):
    # Apply the left shift operator to 1 and the above-given m value and subtract 1 from it.
    # Store it in another variable.
    fnl_numbr = (1 << m) - 1
    # Return the XOR value of the given number and the above result.
    return (gvn_numb ^ fnl_numbr)


# Give the number as static input and store it in a variable.
gvn_numb = 30
# Give the value of m as static input and store it in another variable.
m = 3
# Pass the given number, m value as the arguments to the toglng_lstmbits function.
# Print the number after toggling the given last m bits.
print("The given number{", gvn_numb, "} after toggling the given last m {",
      m, "} bits = ", toglng_lstmbits(gvn_numb, m))

Output:

The given number{ 30 } after toggling the given last m { 3 } bits =  25

Method #2: Using Xor(^) Operator (User Input)

Approach:

  • Give the number as user input using the int(input()) function and store it in a variable.
  • Give the value of m as user input using the int(input()) function and store it in another variable.
  • Pass the given number, m value as the arguments to the toglng_lstmbits function.
  • Create a  function to say toglng_lstmbits which takes the given number, m value as the arguments and returns the number after toggling the given last m bits.
  • Apply the left shift operator to 1 and the above-given m value and subtract 1 from it.
  • Store it in another variable.
  • Return the XOR value of the given number and the above result.
  • Print the number after toggling the given last m bits.
  • The Exit of the Program.

Below is the implementation:

# Create a  function to say toglng_lstmbits which takes the given number, m value as the
# arguments and returns the number after toggling the given last m bits.


def toglng_lstmbits(gvn_numb, m):
    # Apply the left shift operator to 1 and the above-given m value and subtract 1 from it.
    # Store it in another variable.
    fnl_numbr = (1 << m) - 1
    # Return the XOR value of the given number and the above result.
    return (gvn_numb ^ fnl_numbr)


# Give the number as user input using the int(input()) function and store it in a variable.
gvn_numb = int(input("Enter some random number = "))
# Give the value of m as user input using the int(input()) function and 
# store it in another variable.
m = int(input("Enter some random number = "))
# Pass the given number, m value as the arguments to the toglng_lstmbits function.
# Print the number after toggling the given last m bits.
print("The given number{", gvn_numb, "} after toggling the given last m {",
      m, "} bits = ", toglng_lstmbits(gvn_numb, m))

Output:

Enter some random number = 45
Enter some random number = 2
The given number{ 45 } after toggling the given last m { 2 } bits = 46

The best way to learn Python for Beginners is to practice as much as they can taking help of the Sample Python Programs For Beginners. Using them you can develop code on your own and master coding skills.