Get Off-Campus Placement Jobs Info !!!
Programs asked in Mettl Coding Round
Click To Practce List of Programs asked in Nagarro !!!

Program Discussion :: Bitwise Operator

Home > Programs > Bitwise Operator

3 / 15

Write an efficient program to swap the odd and even bits in an integer

Answer:

#include 
#include
using namespace std;
 
unsigned int swapBits(unsigned int x)
{
    // Get all even bits of x
    unsigned int even_bits = x & 0xAAAAAAAA; 

    // Get all odd bits of x
    unsigned int odd_bits = x & 0x55555555; 

    even_bits >>= 1; // Right shift even bits
    odd_bits 

Asked In ::

Post Your Answer Here:

Language:

Post Your Reply Here:



Language:

Post Your Reply Here:



Language:

Post Your Reply Here:



Language:

Post Your Reply Here: