# Python Program to Toggle First and Last Bits of a Number

In the previous article, we have discussed Python Program for Swapping Three Variables Without Using any Temporary Variable.

The task is to toggle the first bit and last bit of a given number 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 =12

Output:

The Number after toggling the first bit and last bit of a given number{ 12 } =  5

Example2:

Input:

Given Number =4

Output:

The Number after toggling the first bit and last bit of a given number{ 4 } =  1

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

Below are the ways to toggle the first bit and last bit of a given number 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 takeLastandFirstsetbits function.
• Create a  function to say takeLastandFirstsetbits 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.
• Add 1 to the given number and right shift the result by 1 and store it in another variable.
• Return the above result +1.
• Pass the given number as an argument to the toggleFirstandLastbits function.
• Create a  function to say toggleFirstandLastbits which takes the given number as an argument and returns the number after toggling the first and last bits of a given number.
• Check if the given number is equal to 1 using the if conditional statement.
• If the statement is true, then return 0.
• Return the Xor result of the given number and takeLastandFirstsetbits(gven_numb).
• Print the number after toggling the first bit and last bit of a given number.
• The Exit of the Program.

Below is the implementation:

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

def takeLastandFirstsetbits(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
# Add 1 to the given number and right shift the result by 1 and store it in another
# variable.
p = (gven_numb + 1) >> 1
# Return the above result +1.
return (p + 1)

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

def toggleFirstandLastbits(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 0.
return 0
# Return the Xor result of the given number and takeLastandFirstsetbits(gven_numb).
return gven_numb ^ takeLastandFirstsetbits(gven_numb)

# Give the number as static input and store it in a variable.
# Pass the given number as an argument to the takeLastandFirstsetbits function.
# Pass the given number as an argument to the toggleFirstandLastbits function.
gven_numb = 12
# Print the number after toggling the first bit and last bit of a given number.
print("The Number after toggling the first bit and last bit of a given number{", gven_numb, "} = ", toggleFirstandLastbits(
gven_numb))


Output:

The Number after toggling the first bit and last bit of a given number{ 12 } =  5

### 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 takeLastandFirstsetbits function.
• Create a  function to say takeLastandFirstsetbits 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.
• Add 1 to the given number and right shift the result by 1 and store it in another variable.
• Return the above result +1.
• Pass the given number as an argument to the toggleFirstandLastbits function.
• Create a  function to say toggleFirstandLastbits which takes the given number as an argument and returns the number after toggling the first and last bits of a given number.
• Check if the given number is equal to 1 using the if conditional statement.
• If the statement is true, then return 0.
• Return the Xor result of the given number and takeLastandFirstsetbits(gven_numb).
• Print the number after toggling the first bit and last bit of a given number.
• The Exit of the Program.

Below is the implementation:

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

def takeLastandFirstsetbits(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
# Add 1 to the given number and right shift the result by 1 and store it in another
# variable.
p = (gven_numb + 1) >> 1
# Return the above result +1.
return (p + 1)

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

def toggleFirstandLastbits(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 0.
return 0
# Return the Xor result of the given number and takeLastandFirstsetbits(gven_numb).
return gven_numb ^ takeLastandFirstsetbits(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 takeLastandFirstsetbits function.
# Pass the given number as an argument to the toggleFirstandLastbits function.
# Print the number after toggling the first bit and last bit of a given number.
print("The Number after toggling the first bit and last bit of a given number{", gven_numb, "} = ", toggleFirstandLastbits(
gven_numb))


Output:

Enter some random number = 4
The Number after toggling the first bit and last bit of a given number{ 4 } = 1

Find the best practical and ready-to-use Python Programming Examples that you can simply run on a variety of platforms and never stop learning.