Python Program to Toggle Bits of a Number Except First and Last bits

The task is to toggle all the bits of a given number except the first and last bits in python.

Toggling: 

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

Examples:

Example1:

Input:

Given Number = 8

Output:

The Number after toggling all the bits of a given number{ 8 } except the first and last bits =  14

Example2:

Input:

Given Number = 15

Output:

The Number after toggling all the bits of a given number{ 15 } except the first and last bits =  9

Program to Toggle Bits of a Number Except First and Last bits in Python

Below are the ways to toggle all the bits of a given number except the first and last bits in python:

Method #1: Using Right Shift (>>)Operator (Static Input)

Approach:

  • Give the number as static input and store it in a variable.
  • Pass the given number as an argument to the setallmidlebits function.
  • Create a  function to say setallmidlebits which takes the given number as an argument.
  • Apply the or(|) operator to the given number and given_numb >> 1(Right Shift) and store it in the same variable.
  • Apply the or(|) operator to the given number and given_numb >> 2(Right Shift) and store it in the same variable.
  • Apply the or(|) operator to the given number and given_numb >> 4(Right Shift) and store it in the same variable.
  • Apply the or(|) operator to the given number and given_numb >> 8(Right Shift) and store it in the same variable.
  • Apply the or(|) operator to the given number and given_numb >> 16(Right Shift) and store it in the same variable.
  • Right shift the given number by 1 and store it in another variable.
  • Return the Xor value of the above result and 1.
  • Pass the given number as an argument to the toglemidlebits function.
  • Create a  function to say toglemidlebits which takes the given number as an argument and returns the number after toggling all the bits of a given number except the first and last bits.
  • Check if the given number is equal to 1 using the if conditional statement.
  • If the statement is true, then return 1.
  • Return the Xor result of the given number and setallmidlebits(gven_numb).
  • Print the number after toggling all the bits of a given number except the first and last bits.
  • The Exit of the Program.

Below is the implementation:

# Create a  function to say setallmidlebits which takes the given number as an
# argument.


def setallmidlebits(gven_numb):
    # Apply the or(|) operator to the given number and given_numb >> 1(Right Shift) and
    # store it in the same variable.
    gven_numb = gven_numb | gven_numb >> 1
    # Apply the or(|) operator to the given number and given_numb >> 2(Right Shift) and
    # store it in the same variable.
    gven_numb = gven_numb | gven_numb >> 2
    # Apply the or(|) operator to the given number and given_numb >> 4(Right Shift) and
    # store it in the same variable.
    gven_numb = gven_numb | gven_numb >> 4
    # Apply the or(|) operator to the given number and given_numb >> 8(Right Shift) and
    # store it in the same variable.
    gven_numb = gven_numb | gven_numb >> 8
    # Apply the or(|) operator to the given number and given_numb >> 16(Right Shift) and
    # store it in the same variable.
    gven_numb = gven_numb | gven_numb >> 16
    # Right shift the given number by 1 and store it in another variable.
    p = (gven_numb >> 1)
    # Return the Xor value of the above result and 1.
    return (p ^ 1)

# Create a  function to say toglemidlebits which takes the given number as an
# argument and returns the number after toggling all the bits of a given number except the
# first and last bits.


def toglemidlebits(gven_numb):
  # Check if the given number is equal to 1 using the if conditional statement.
    if (gven_numb == 1):
      # If the statement is true, then return 1.
        return 1
    # Return the Xor result of the given number and setallmidlebits(gven_numb).
    return gven_numb ^ setallmidlebits(gven_numb)


# Give the number as static input and store it in a variable.
gven_numb = 8
# Pass the given number as an argument to the setallmidlebits function.
# Pass the given number as an argument to the toglemidlebits function.
# Print the number after toggling all the bits of a given number except the
# first and last bits.
print("The Number after toggling all the bits of a given number{", gven_numb,
      "} except the first and last bits = ", toglemidlebits(gven_numb))

Output:

The Number after toggling all the bits of a given number{ 8 } except the first and last bits =  14

Method #2: Using Right Shift (>>)Operator (User Input)

Approach:

  • Give the number as user input using the int(input()) function and store it in a variable.
  • Pass the given number as an argument to the setallmidlebits function.
  • Create a  function to say setallmidlebits which takes the given number as an argument.
  • Apply the or(|) operator to the given number and given_numb >> 1(Right Shift) and store it in the same variable.
  • Apply the or(|) operator to the given number and given_numb >> 2(Right Shift) and store it in the same variable.
  • Apply the or(|) operator to the given number and given_numb >> 4(Right Shift) and store it in the same variable.
  • Apply the or(|) operator to the given number and given_numb >> 8(Right Shift) and store it in the same variable.
  • Apply the or(|) operator to the given number and given_numb >> 16(Right Shift) and store it in the same variable.
  • Right shift the given number by 1 and store it in another variable.
  • Return the Xor value of the above result and 1.
  • Pass the given number as an argument to the toglemidlebits function.
  • Create a  function to say toglemidlebits which takes the given number as an argument and returns the number after toggling all the bits of a given number except the first and last bits.
  • Check if the given number is equal to 1 using the if conditional statement.
  • If the statement is true, then return 1.
  • Return the Xor result of the given number and setallmidlebits(gven_numb).
  • Print the number after toggling all the bits of a given number except the first and last bits.
  • The Exit of the Program.

Below is the implementation:

# Create a  function to say setallmidlebits which takes the given number as an
# argument.


def setallmidlebits(gven_numb):
    # Apply the or(|) operator to the given number and given_numb >> 1(Right Shift) and
    # store it in the same variable.
    gven_numb = gven_numb | gven_numb >> 1
    # Apply the or(|) operator to the given number and given_numb >> 2(Right Shift) and
    # store it in the same variable.
    gven_numb = gven_numb | gven_numb >> 2
    # Apply the or(|) operator to the given number and given_numb >> 4(Right Shift) and
    # store it in the same variable.
    gven_numb = gven_numb | gven_numb >> 4
    # Apply the or(|) operator to the given number and given_numb >> 8(Right Shift) and
    # store it in the same variable.
    gven_numb = gven_numb | gven_numb >> 8
    # Apply the or(|) operator to the given number and given_numb >> 16(Right Shift) and
    # store it in the same variable.
    gven_numb = gven_numb | gven_numb >> 16
    # Right shift the given number by 1 and store it in another variable.
    p = (gven_numb >> 1)
    # Return the Xor value of the above result and 1.
    return (p ^ 1)

# Create a  function to say toglemidlebits which takes the given number as an
# argument and returns the number after toggling all the bits of a given number except the
# first and last bits.


def toglemidlebits(gven_numb):
  # Check if the given number is equal to 1 using the if conditional statement.
    if (gven_numb == 1):
      # If the statement is true, then return 1.
        return 1
    # Return the Xor result of the given number and setallmidlebits(gven_numb).
    return gven_numb ^ setallmidlebits(gven_numb)


# Give the number as user input using the int(input()) function and store it in a variable.
gven_numb = int(input("Enter some random number = "))
# Pass the given number as an argument to the setallmidlebits function.
# Pass the given number as an argument to the toglemidlebits function.
# Print the number after toggling all the bits of a given number except the
# first and last bits.
print("The Number after toggling all the bits of a given number{", gven_numb,
      "} except the first and last bits = ", toglemidlebits(gven_numb))

Output:

Enter some random number = 15
The Number after toggling all the bits of a given number{ 15 } except the first and last bits = 9

Leave a Comment