Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //{ Driver Code Starts
- //Initial Template for C++
- #include<bits/stdc++.h>
- using namespace std;
- // } Driver Code Ends
- //User function Template for C++
- class Solution
- {
- public:
- //Function to swap odd and even bits.
- unsigned int swapBits(unsigned int n)
- {
- int oddm=0x55555555;
- int evenm=0xAAAAAAAA;
- int odd=n&oddm;
- int even=n&evenm;
- odd=odd<<1;
- even=even>>1;
- n=odd|even;
- return n;
- }
- };
- //{ Driver Code Starts.
- // Driver code
- int main()
- {
- int t;
- cin>>t;//testcases
- while(t--)
- {
- unsigned int n;
- cin>>n;//input n
- Solution ob;
- //calling swapBits() method
- cout << ob.swapBits(n) << endl;
- }
- return 0;
- }
- // } Driver Code Ends
Advertisement
Add Comment
Please, Sign In to add comment