[Updated] Goldman Sachs Aptitude Test Questions and Answers
Practice List of TCS Digital Coding Questions !!!
Take 50+ FREE!! Online Data Interpretation Mock test to crack any Exams.

Program Discussion :: Bitwise Operator

Home > Programs > Bitwise Operator

14 / 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: