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
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
Do you know bitwise operations?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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 >>.
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.
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.
Actually, you can swap bits, it's just a little more difficult.
For example:
Caution: Untested! May blow up your PC!Code:bit1 = byte & ~(0x1 << bit1place); bit2 = byte & ~(0x1 << bit2place); byte &= ~(0x1 << bit1place) & ~(0x1 << bit2place); byte |= (bit1 >> (bit1place - bit2place)) | (bit2 >> (bit2place - bit1place));
Devoted my life to programming...
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).
And how this works.Code:unsigned int a = 20; unsigned int msk = 6; a ^= msk;
Code:10100 00110
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
Hope is the first step on the road to disappointment.
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?
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.
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; if( (*a & mask) && (*a & mask) != mask ) { *a ^= mask; } }
My example does two times more computations than yours: Wanna bet mine's faster?
Devoted my life to programming...