Swap Even Odd Bits

Bit Manipulation Easy Coding Problem Bits Algorithm

Problem Description

Given a non-negative integer, write a function to swap all even bits with odd bits. That is, bit 0 should be swapped with bit 1, bit 2 with bit 3, and so on.

For example:

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

Your task is to implement the function that performs this bit swap operation. Aim to optimize your solution using bit manipulation techniques.

Hint: Use bit masks to separate the even and odd bits, then shift them accordingly before combining them back.

Note: In some regions of the world, May 1 (May Day) is celebrated as International Workers' Day, a reminder of the value of thoughtful and efficient work. Enjoy coding!