Given a number x and two positions (from the right side) in the binary representation of x, write a function that swaps n bits at given two positions and returns the result. It is also given that the two sets of bits do not overlap.
Let p1 and p2 be the two given positions.
Input: x = 47 (00101111) p1 = 1 (Start from the second bit from the right side) p2 = 5 (Start from the 6th bit from the right side) n = 3 (No of bits to be swapped) Output: 227 (11100011) The 3 bits starting from the second bit (from the right side) are swapped with 3 bits starting from 6th position (from the right side)
Input: x = 28 (11100) p1 = 0 (Start from first bit from right side) p2 = 3 (Start from 4th bit from right side) n = 2 (No of bits to be swapped) Output: 7 (00111) The 2 bits starting from 0th position (from right side) are swapped with 2 bits starting from 4th position (from right side)
We need to swap two sets of bits. XOR can be used in a similar way as it is used to swap 2 numbers. Following is the algorithm.
1) Move all bits of the first set to the rightmost side set1 = (x >> p1) & ((1U << n) - 1) Here the expression (1U << n) - 1 gives a number that contains last n bits set and other bits as 0. We do & with this expression so that bits other than the last n bits become 0. 2) Move all bits of second set to rightmost side set2 = (x >> p2) & ((1U << n) - 1) 3) XOR the two sets of bits xor = (set1 ^ set2) 4) Put the xor bits back to their original positions. xor = (xor << p1) | (xor << p2) 5) Finally, XOR the xor with original number so that the two sets are swapped. result = x ^ xor
Result = 7
Following is a shorter implementation of the same logic
Method 2 –
This solution focuses on calculating the values of bits to be swapped using AND gate. Then we can set/unset those bits based on whether the bits are to be swapped. For the number of bits to be swapped (n) –
- Calculate shift1 = The value after setting bit at p1 position to 1
- Calculate shift2 = The value after setting bit at p2 position to 1
- value1 = Number to check if num at position p1 is set or not.
- value2 = Number to check if num at position p2 is set or not.
- If value1 and value2 are different is when we have to swap the bits.
[28 0 3 2] num=28 (11100) p1=0 p2=3 n=2 Given = 11100 Required output = 00111 i.e. (00)1(11) msb 2 bits replaced with lsb 2 bits n=2 p1=0, p2=3 shift1= 1, shift2= 1000 value1= 0, value2= 1000 After swap num= 10101 n=3 p1=1, p2=4 shift1= 10, shift2= 10000 value1= 0, value2= 10000 After swap num= 00111
Result = 7
Swapping individual bits with XOR
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- How to swap two bits in a given integer?
- Swap all odd and even bits
- Swap every two bits in bytes
- Check if bits of a number has count of consecutive set bits in increasing order
- Toggle bits of a number except first and last bits
- Check if all bits can be made same by flipping two consecutive bits
- Print numbers having first and last bits as the only set bits
- Maximize a given unsigned number number by swapping bits at it's extreme positions.
- Minimum number using set bits of a given number
- Number of ways to swap two bit of s1 so that bitwise OR of s1 and s2 changes
- Bit manipulation | Swap Endianness of a number
- Set all the bits in given range of a number
- Reverse actual bits of the given number
- Smallest number whose set bits are maximum in a given range
- Count number of bits changed after adding 1 to given N
- Unset least significant K bits of a given number
- Extract 'k' bits from a given position in a number.
- Next higher number with same number of set bits
- M-th smallest number having k number of set bits.
- Number of integers with odd number of set bits