Swap Even Odd Bits

Bit Manipulation Bitwise Swap

Problem Description

Write a function that takes a non-negative integer and swaps its even and odd bits. For example, the bit at position 0 should be swapped with position 1, the bit at position 2 with position 3, and so on.

Example:

  • Input: 23 (in binary: 10111)
  • Output: 43 (in binary: 101011)

Note: The result might require more bits than the input representation.

Today, on November 17, as we recognize historical moments and special days, challenge yourself by mastering bit manipulation. This problem is a great way to get comfortable with bitwise operators and improve your problem-solving skills.

Function Signature:

The function should accept an integer and return the new integer after swapping its even and odd bits.

Constraints:

  • The input is a non-negative integer.
  • Aim for an efficient solution using bitwise operations.

Happy coding!