10 Replies Latest reply: Sep 26, 2010 10:15 PM by 800717

# Using bitwise operators to swap individual bits

I'm able to set or clear individual bits using masks, however I'm trying to figure out how I could go about swapping two bits using bitwise operators but I haven't been able to figure it out. For example, say I had int x = 37 which is 100101 in binary and I wanted to swap the 2nd and 3rd bits I'd like to get 100011 when I use Integer.toBinaryString(x) after performing some bitwise operations on x to swap bits around. Any suggestions ?
• ###### 1. Re: Using bitwise operators to swap individual bits
May not be the best way
``````int x = 37;
System.out.println(Integer.toBinaryString(x)); // 100101

int reversedBits2and3 = ((x / 2) % 2) * 4 + ((x / 4) % 2) * 2;
int blankBits2and3 = Integer.parseInt("11111001", 2);
int swappedBits2and3 = (x & blankBits2and3) | reversedBits2and3;

System.out.println(Integer.toBinaryString(swappedBits2and3)); //100011``````
db
• ###### 2. Re: Using bitwise operators to swap individual bits
I love this question! Quick answer: use the Exclusive OR operator.
``int result = (37 ^ 6);``
• ###### 3. Re: Using bitwise operators to swap individual bits
pierrot_2 wrote:
I love this question! Quick answer: use the Exclusive OR operator.
?:| Doesn't that toggle, rather than swap, bits 2 and 3?

db
• ###### 4. Re: Using bitwise operators to swap individual bits
Now I'm confused about what OP meant by swapping... This definitely calls for a method.
• ###### 5. Re: Using bitwise operators to swap individual bits
OP, what you gonna do when the two bits you want to swap are both 0 or 1? Is that the kind of swapping we do around here? If it is true, then it's time to swap the decks... aye Mr. Burke?
• ###### 6. Re: Using bitwise operators to swap individual bits
If both positions are 0 or 1 then the value would be unchanged. For example 101001 swapping the 1st and 4th bits, 101001, the 1 in the 4th position would get "copied" into the 1st position, and the 1 in the 1st position would get "copied" into the 4th position thus no change would really take place. Whereas 100001 would result in swapping the 1st and 4th position to become 101000

Edited by: 797714 on Sep 26, 2010 3:50 PM
• ###### 7. Re: Using bitwise operators to swap individual bits
797714 wrote:
I'm able to set or clear individual bits using masks, however I'm trying to figure out how I could go about swapping two bits using bitwise operators but I haven't been able to figure it out. For example, say I had int x = 37 which is 100101 in binary and I wanted to swap the 2nd and 3rd bits I'd like to get 100011 when I use Integer.toBinaryString(x) after performing some bitwise operations on x to swap bits around. Any suggestions ?
I remember when making a chess engine we used bitboards and we had to something much like this. Unfortunatley I never hung onto the source code but I do remember all we had to use were bitwise operators. If you could tell me what you are trying to do exactly maybe I can formulate a better way to go about what you are trying to do.
• ###### 8. Re: Using bitwise operators to swap individual bits
Did not compile and run, but try something like this:
``````byte swapBits(byte b, int bit1Pos, int bit2Pos) { // Bit positions are from 7 (MSB) to 0 (LSB)
byte mask1 = 1 << bit1Pos;
byte mask2 = 1 << bit2Pos;

byte result = b;

return result;
}``````
Edited by: baftos on Sep 26, 2010 9:03 PM
• ###### 9. Re: Using bitwise operators to swap individual bits
How about something like this? A preliminary test seemed to work.
``````class BitSwap2 {
public static void main(String[] argv) {
int x = 37;
System.out.printf("Binary: %s\n", Integer.toBinaryString(x));

swapBits(x, 2, 3);
}

private static void swapBits(int x, int a, int b) {
if(a == b) {
System.out.printf("Nothing to swap.\n");
return;
}

int first = (x & (1<<(a-1)));
int second = (x & (1<<(b-1)));

// zero them out from the original so that we can swap them
x ^= (first | second);

if(a > b) {
int diff = (a - b);
x |= (first >> diff);
x |= (second << diff);
} else {
int diff = (b - a);
x |= (second >> diff);
x |= (second << diff);
}

System.out.printf("Binary: %s\n", Integer.toBinaryString(x));
}
}``````
• ###### 10. Re: Using bitwise operators to swap individual bits
Thanks for the tips above, managed to get it working with some of the suggestions.