Python Program to Swap all Odd and Even Bits of a Number

Given a number, the task is to swap all odd and even bits of the given number in Python.

In binary, the number 185 is represented as 10111001. The bits in bold are in even positions, and they are 1 1 1 0, while the bits in odd positions are 0 1 0 1.

After swapping the odd and even bits, we get 118(1110110)

Examples:

Example1:

Input:

Given Number =456

Output:

The original given number is = 456
The modified number after swapping bits is = 708

Example2:

Input:

Given Number =185

Output:

The original given number is = 185
The modified number after swapping bits is = 118

Program to Swap all Odd and Even Bits of a Number in Python

Below are the ways to swap all odd and even bits of the given number in Python.

The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.

Method #1: Using Binary Operators (Static Input)

Approach:

  • Give the number as static input and store it in a variable.
  • To extract the odd bits from the number, use a bitwise AND operation with hexadecimal 55555555.
  • To extract the even bits from the number, use a bitwise AND operation with hexadecimal AAAAAAAA.
  • Perform a left shift << by 1 position to move odd bits to even positions.
  • Perform a right shift >> by 1 place to move even bits to odd positions.
  • Finally, using the bitwise OR operator, to combine both bits.
  • Print the modified Number after swapping odd and even bits of the given number.
  • The Exit of the Program.

Below is the implementation:

# Give the number as static input and store it in a variable.
numb = 185
# To extract the odd bits from the number,
# use a bitwise AND operation with hexadecimal 55555555.
oddbitsnumb = numb & 0x55555555
# To extract the even bits from the number,
# use a bitwise AND operation with hexadecimal AAAAAAAA.
evenbitsnumb = numb & 0xAAAAAAAA
# Perform a left shift << by 1 position to move odd bits to even positions.
oddbitsnumb = oddbitsnumb << 1
# Perform a right shift >> by 1 place to move even bits to odd positions.
evenbitsnumb = evenbitsnumb >> 1
# Finally, using the bitwise OR operator, to combine both bits.
modifdnumb = oddbitsnumb | evenbitsnumb
# Print the modified Number after swapping odd and even bits of the given number.
print('The original given number is =', numb)
print('The modified number after swapping bits is =', modifdnumb)

Output:

The original given number is = 185
The modified number after swapping bits is = 118

Method #2: Using Binary Operators (User Input)

Approach:

  • Give the number as user input using int(input()) and store it in a variable.
  • To extract the odd bits from the number, use a bitwise AND operation with hexadecimal 55555555.
  • To extract the even bits from the number, use a bitwise AND operation with hexadecimal AAAAAAAA.
  • Perform a left shift << by 1 position to move odd bits to even positions.
  • Perform a right shift >> by 1 place to move even bits to odd positions.
  • Finally, using the bitwise OR operator, to combine both bits.
  • Print the modified Number after swapping odd and even bits of the given number.
  • The Exit of the Program.

Below is the implementation:

# Give the number as user input using int(input()) and store it in another variable.
numb = int(input('Enter some random number = '))
# To extract the odd bits from the number,
# use a bitwise AND operation with hexadecimal 55555555.
oddbitsnumb = numb & 0x55555555
# To extract the even bits from the number,
# use a bitwise AND operation with hexadecimal AAAAAAAA.
evenbitsnumb = numb & 0xAAAAAAAA
# Perform a left shift << by 1 position to move odd bits to even positions.
oddbitsnumb = oddbitsnumb << 1
# Perform a right shift >> by 1 place to move even bits to odd positions.
evenbitsnumb = evenbitsnumb >> 1
# Finally, using the bitwise OR operator, to combine both bits.
modifdnumb = oddbitsnumb | evenbitsnumb
# Print the modified Number after swapping odd and even bits of the given number.
print('The original given number is =', numb)
print('The modified number after swapping bits is =', modifdnumb)

Output:

Enter some random number = 456
The original given number is = 456
The modified number after swapping bits is = 708

Related Programs: