|
| 1 | +/** |
| 2 | + * Problem - Given a number 'num' and two positions (p1 and p2 from right side) in binary representation of num, |
| 3 | + * write a function that swaps n bits at given two positions and returns the result. |
| 4 | + * It is also given that the two sets of bits do not overlap. |
| 5 | + */ |
| 6 | + |
| 7 | +#include <iostream> |
| 8 | + |
| 9 | +int swapBits(unsigned int num, unsigned int p1, |
| 10 | + unsigned int p2, unsigned int n ) |
| 11 | +{ |
| 12 | + //Step1 lets form a number set1 by moving n bits at position p1 to the rightmost |
| 13 | + unsigned int set1 = (num >> p1 ) & ((1U << n) - 1); |
| 14 | + // Lets understand what we just did. |
| 15 | + // Part1 : (num >> p1), we just right shifted num so that bit at p1 position takes 0th postion. |
| 16 | + // Part2 : Remember we needed n bits from position p1(which has become position 0) |
| 17 | + // So, (1U << n) moves 1 to nth bit, i.e. it the value formed by this movement is 2^n |
| 18 | + // Now, If we substract 1 from the (2^n), it will give us all 1's for n positions. |
| 19 | + // For example (1U << 3) = 1000 |
| 20 | + // and (1U << 3) - 1 = 0111 |
| 21 | + // Part3 : Thus Part1 & Part2 will give as n bits which were at P1 (now moved to 0) |
| 22 | + |
| 23 | + //similarly for p2 |
| 24 | + unsigned int set2 = (num >> p2) & ((1U << n) - 1); |
| 25 | + |
| 26 | + // xor two sets ( we are doing similar to xor swap algorithm ) |
| 27 | + // https://en.wikipedia.org/wiki/XOR_swap_algorithm |
| 28 | + unsigned int xorSets = set1 ^ set2; |
| 29 | + |
| 30 | + // now moving back the xor'd sets to p1 and p2 |
| 31 | + xorSets = (xorSets << p1) | (xorSets << p2); |
| 32 | + |
| 33 | + unsigned int finalVal = xorSets ^ num; |
| 34 | + return finalVal; |
| 35 | +} |
| 36 | + |
| 37 | +int main() |
| 38 | +{ |
| 39 | + std::cout << "Swaping bits in number 28, such that 2 bits starting from 0th bit and 2 bits " |
| 40 | + "starting from 3rd bit are swapped, 28 becomes " << swapBits(28, 0, 3, 2) |
| 41 | + << std::endl; |
| 42 | + |
| 43 | + std::cout << "Swaping bits in number 47, such that 3 bits starting from 1st bit(0 based counting) and 3 bits " |
| 44 | + "starting from 5th bit(0 based counting) are swapped, 47 becomes " << swapBits(47, 1, 5, 3) |
| 45 | + << std::endl; |
| 46 | +} |
0 commit comments