Welcome Back Reader!
In this article we will discuss the next problem based on 'Bit Manipulation' i.e. 'Swap All Odd And Even Bits'. This is a very interesting problem
Before you move any further, it is advised that you give this problem a fair try.
Important Links : Problem Link, Solution video
In this problem you are given an array of integers.
All you need to do is to swap all odd position bits with even position bits.
Every odd position bit is swapped with the adjacent bit on the left side and every even position bit is swapped with the adjacent bit on the right side.
Print the number formed after swapping.
For example:
Sample Input: 15
Sample Output: 13
How?
Well the answer would be:
For more clarity of the question, watch part of the video.
Well, this was quite easy. Wasn't it?
For more clarity of this part, watch part of the video.
- The problem here deals with the swapping of all even position bits with odd position bits and then finally returning the resultant number thus formed.
- So we basically need to swap the digits of two adjacent indices i.e. 0 with 1, 2 with 3, ..... (2k - 1) with (2k).
- The problem statement can be reframed as given a number N, we need to shift all its even position bits by 1 to the right side and all its odd position bits by 1 to its left side.
- To achieve this we will be maintaining two integers one will be storing the result of all even bits and the other one will be storing the result of all odd bits.
- If you can observe that even bits are of type ...1010...1010 which is the same as 0x...A...A... (as Hexadecimal value of A = 1010) so we can take AND operation with 0xAAAAAAAA to get all even bits.
- Similarly, we can take the AND operation result of N with 0x555555555 to get our odd bits as 0x55 is the same as 01010101 (as Hexadecimal value of 5 = 0101) which implies that this contains 1 only at odd positions.
- Now, as soon as we have our bits separated we just need to left shift odd bits and right shift even bits and take their OR to get the final result.
For more clarity of this part, watch part of the video.
Let's try to code this!
//1 - First of all we take two variables om (odd mask) and em (even mask) and initialize these by 0x55555555 and 0xAAAAAAAA respectively.
//2 - Then we collect the odd and even bits by performing operations on the number and odd and even mask respectively.
//3 - Then we left shift the collected odds number by 1 and right shift the even numbers by 1.
//4 - Then we perform OR operation between the odd and even numbers and update the change in number.
//5 - And at last simply print the number.
For more clarity of this part, watch part of the video.
java; true";
The time complexity for the function is linear as we are traversing on every element of the array.
The space complexity for the function is constant.
We hope that this article was helpful. If somehow you are finding it difficult to
understand this problem then we advise you to watch our
Trust me it will just get easier to understand after you have watched the solution video.
You can contact us via our website. Doubts, suggestions and feedback are always welcomed.
It is also advised that you follow the sequence of modules and questions which is there on our website.
Keep practicing more and more problems daily. Meditate enough on each step of each problem.
All the best for an optimistic future!
Happy Coding!