Like Tree6Likes

Swap between bits?

This is a discussion on Swap between bits? within the C Programming forums, part of the General Programming Boards category; Hello I have to swap between bits. I have byte (type of char) variable. I have to swap between 2nd ...

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    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. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,774
    Do you know bitwise operations?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,498
    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. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,709
    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.
    stahta01 likes this.

  5. #5
    Registered User
    Join Date
    Jan 2012
    Posts
    69
    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. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    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!
    Devoted my life to programming...

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    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
    stahta01 likes this.

  8. #8
    Registered User
    Join Date
    Jan 2012
    Posts
    69
    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. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by kevinstrijbos View Post
    Well, you can swap bits, but manually?
    I'm not sure what this question is.
    Quote Originally Posted by kevinstrijbos View Post
    And Greaper, you can't LITERALLY swap bits in a number.
    Sure you can.
    Quote Originally Posted by kevinstrijbos View Post
    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.
    Subsonics likes this.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Jan 2012
    Posts
    69
    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. #11
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by kevinstrijbos View Post
    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. #12
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    Quote Originally Posted by Subsonics View Post
    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?
    Devoted my life to programming...

  13. #13
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,498
    Quote Originally Posted by GReaper View Post
    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. #14
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by GReaper View Post
    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;
    
        if( (*a & mask) && (*a & mask) != mask ) {
            *a ^= mask;
        }
    }

  15. #15
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    My example does two times more computations than yours: Wanna bet mine's faster?
    Devoted my life to programming...

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extracting certain bits from sequence of bits
    By lucaspewkas in forum C Programming
    Replies: 5
    Last Post: 10-06-2007, 12:22 AM
  2. off by one and help with swap
    By jrb47 in forum C++ Programming
    Replies: 1
    Last Post: 11-11-2006, 06:45 PM
  3. swap()? (g++)
    By jafet in forum C++ Programming
    Replies: 4
    Last Post: 08-05-2006, 04:59 PM
  4. New idea on conveting byte to bits/bits to byte
    By megablue in forum C Programming
    Replies: 10
    Last Post: 10-26-2003, 01:16 AM
  5. copy some bits into a 8 bits binary number
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 05-29-2002, 10:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21