Swap Even-Odd Bits

Bit Manipulation Easy Swapping

Swap Even-Odd Bits

Given a non-negative integer, swap its even and odd bits. In other words, the bit at position 0 should be swapped with the bit at position 1, the bit at position 2 with the bit at position 3, and so on.

For example:

  • If the input is 10 (which is 1010 in binary), swapping its even and odd bits gives 5 (0101 in binary).
  • If the input is 14 (which is 1110 in binary), the output should be 13 (1101 in binary).

Hint: You can use bit masking and shifting to extract even and odd bits, then combine them after swapping.

This problem pays tribute to the innovative days of early computer engineering. Happy coding!