{"id":13672,"date":"2021-10-01T10:30:39","date_gmt":"2021-10-01T05:00:39","guid":{"rendered":"https:\/\/python-programs.com\/?p=13672"},"modified":"2021-11-22T18:33:28","modified_gmt":"2021-11-22T13:03:28","slug":"python-program-to-swap-all-odd-and-even-bits-of-a-number","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-swap-all-odd-and-even-bits-of-a-number\/","title":{"rendered":"Python Program to Swap all Odd and Even Bits of a Number"},"content":{"rendered":"
Given a number, the task is to swap all odd and even bits of the given number in Python.<\/p>\n
In binary, the number 185 is represented as 10111001. The bits in bold are in even positions, and they are 1 1 1 0<\/strong>, while the bits in odd positions are 0 1 0 1<\/strong>.<\/p>\n After swapping the odd and even bits, we get 118(1110110)<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to swap all odd and even bits of the given number in Python.<\/p>\n The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples<\/a> as it includes basic to advanced levels of concepts.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n 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 …<\/p>\nGiven Number =456<\/pre>\n
The original given number is = 456\r\nThe modified number after swapping bits is = 708<\/pre>\n
Given Number =185<\/pre>\n
The original given number is = 185\r\nThe modified number after swapping bits is = 118<\/pre>\n
Program to Swap all Odd and Even Bits of a Number in Python<\/h2>\n
\n
Method #1: Using Binary Operators (Static Input)<\/h3>\n
\n
# Give the number as static input and store it in a variable.\r\nnumb = 185\r\n# To extract the odd bits from the number,\r\n# use a bitwise AND operation with hexadecimal 55555555.\r\noddbitsnumb = numb & 0x55555555\r\n# To extract the even bits from the number,\r\n# use a bitwise AND operation with hexadecimal AAAAAAAA.\r\nevenbitsnumb = numb & 0xAAAAAAAA\r\n# Perform a left shift << by 1 position to move odd bits to even positions.\r\noddbitsnumb = oddbitsnumb << 1\r\n# Perform a right shift >> by 1 place to move even bits to odd positions.\r\nevenbitsnumb = evenbitsnumb >> 1\r\n# Finally, using the bitwise OR operator, to combine both bits.\r\nmodifdnumb = oddbitsnumb | evenbitsnumb\r\n# Print the modified Number after swapping odd and even bits of the given number.\r\nprint('The original given number is =', numb)\r\nprint('The modified number after swapping bits is =', modifdnumb)\r\n<\/pre>\n
The original given number is = 185\r\nThe modified number after swapping bits is = 118<\/pre>\n
Method #2: Using Binary Operators (User Input)<\/h3>\n
\n
# Give the number as user input using int(input()) and store it in another variable.\r\nnumb = int(input('Enter some random number = '))\r\n# To extract the odd bits from the number,\r\n# use a bitwise AND operation with hexadecimal 55555555.\r\noddbitsnumb = numb & 0x55555555\r\n# To extract the even bits from the number,\r\n# use a bitwise AND operation with hexadecimal AAAAAAAA.\r\nevenbitsnumb = numb & 0xAAAAAAAA\r\n# Perform a left shift << by 1 position to move odd bits to even positions.\r\noddbitsnumb = oddbitsnumb << 1\r\n# Perform a right shift >> by 1 place to move even bits to odd positions.\r\nevenbitsnumb = evenbitsnumb >> 1\r\n# Finally, using the bitwise OR operator, to combine both bits.\r\nmodifdnumb = oddbitsnumb | evenbitsnumb\r\n# Print the modified Number after swapping odd and even bits of the given number.\r\nprint('The original given number is =', numb)\r\nprint('The modified number after swapping bits is =', modifdnumb)\r\n<\/pre>\n
Enter some random number = 456\r\nThe original given number is = 456\r\nThe modified number after swapping bits is = 708<\/pre>\n
\n