Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

write a program to swap all odd bits with next even bits of integer. how do i get

+1 vote
asked Nov 19, 2018 by Harshavardhan (130 points)

1 Answer

0 votes
answered Nov 22, 2018 by gurik
Something like this?

#include<iostream>
#include<bitset>

using namespace std;

const size_t bit_num = 8;

int main ()
{
  bitset<bit_num> num(42); // for example

  cout << "Num: " << num << endl;
 
  for(size_t i = 0; i < bit_num; i += 2) {
      bitset<1> temp(num[i]);
      num[i]  = num[i + 1];
      num[i + 1] = temp[0];
  }
 
  cout << "Num: " << num << endl;
 
  return 0;
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...