1. ## Swap between bits?

Hello
I have to swap between bits.
I have byte (type of char) variable.
I have to swap between 2nd bit and 4bit (for example)
I have no idea how to do this .. Does anybody have idea?

Thanks

2. Do you know bitwise operations?

3. You need to use bitwise operators, like & (and), | (or) and the left and right shift operators (<< and >>). Find your favorite C book/reference and read up on them. Google is your friend. Also, you can read up on them here: Tutorials - Bitwise Operators and Bit Manipulations in C and C++ - Cprogramming.com, though there's no info on << or >>.

4. You need to check one of the bits to see if it's on, and if the other is off, you need to toggle them both.

5. Use bitwise operators.

Then, since you can't literally swap bits, calculate the powers (so 2^n) of the bits you need to swap, and manually add and subtract these powers.

6. Actually, you can swap bits, it's just a little more difficult.
For example:
Code:
```bit1 = byte & ~(0x1 << bit1place);
bit2 = byte & ~(0x1 << bit2place);

byte &= ~(0x1 << bit1place) & ~(0x1 << bit2place);
byte |= (bit1 >> (bit1place - bit2place)) | (bit2 >> (bit2place - bit1place));```
Caution: Untested! May blow up your PC!

7. You can swap bits with xor and a mask. If you want to swap bit 2 and 3, create a mask with the value 6 (110).

Code:
```unsigned int a = 20;
unsigned int msk = 6;

a ^= msk;```
And how this works.

Code:
```10100
00110```

8. Well, you can swap bits, but manually? You can also use the complement operator then...

And Greaper, you can't LITERALLY swap bits in a number.

For example; You got the number 10 (so 0000 1010), you can't make the 1 a 0, 10 is a constant

9. Originally Posted by kevinstrijbos
Well, you can swap bits, but manually?
I'm not sure what this question is.
Originally Posted by kevinstrijbos
And Greaper, you can't LITERALLY swap bits in a number.
Sure you can.
Originally Posted by kevinstrijbos
For example; You got the number 10 (so 0000 1010), you can't make the 1 a 0, 10 is a constant
You are confusing lvalues and rvalues.
Code:
```unsigned char x = 10, y = 1;
x = x ^ y;```
X has now had its bits swapped.

Quzah.

10. Yes Quzah, you can do it with a mask and bitwise operators I know.
But I mean that you can't literaly change the bits, you can't say to C: well, here's a one, change it into a zero.
You know what I mean?

11. Originally Posted by kevinstrijbos
But I mean that you can't literaly change the bits, you can't say to C: well, here's a one, change it into a zero.
Yes you can, but not with a tiny tweezer and a strong magnifying glass.

Code:
```/* counting from 0 and up, lsb is 0 */
void flip_bit(unsigned int *a, unsigned int pos) {

pos = 1 << pos;
*a ^= pos;
}```

12. Originally Posted by Subsonics
You can swap bits with xor and a mask. If you want to swap bit 2 and 3, create a mask with the value 6 (110).

Code:
```unsigned int a = 20;
unsigned int msk = 6;

a ^= msk;```
And how this works.

Code:
```10100
00110```
Umm, this actually doesn't work. What happens if both bits are 1 or both are 0?

13. Originally Posted by GReaper
Umm, this actually doesn't work. What happens if both bits are 1 or both are 0?
You imagined the kind of bit swapping I did. Where, e.g., you take the value in bit 4 and the value in bit 1 and swap them, so bit 4 now has bit 1's value and vice-versa. Everybody else seems to be talking about toggling or flipping bits. For that purpose, Subsonic's code does work.

14. Originally Posted by GReaper
Umm, this actually doesn't work. What happens if both bits are 1 or both are 0?
Yes it does. What is the point in swapping two bits with the same value? The mask is set, so that it matches the bits you want to swap.

If you want to create a fool proof version you could check if they are set before hand, but, the example I gave is just showing how this can be done in principle.

For a fool proof version, using the same concept.

Code:
```void bitswap2(unsigned int *a, unsigned int b1, unsigned int b2) {

b1 = 1 << b1;
b2 = 1 << b2;
unsigned int mask = b1 | b2;