Thread: Flipping a bit at position p

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    269

    Flipping a bit at position p

    Hi, suppose I have a sequence of 64bits, i.e., a uint64_t. The goal is to flip a bit at position p of the bit sequence. How can I do this with C? I've read about masks and stuff, but masks only are fixed, where as I need something more generic.

    For example, bit sequence: 110001 and I want to flip bit in the 1 position, I would get 110011. Flipping a bit in the 3rd position, I would get 111001.


    Many thanks.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You can use exclusive-OR with a mask to toggle a bit.

    You can roll this into a macro, and add bit-shifting to create the mask for the desired position.

    Code:
    #define BIT_FLIP(x,y)  /* fill in the blank; 'x' is the binary pattern, 'y' is the bit position */

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Take the value "1" and left-shift it to the position you want, then XOR that with the value to flip the bit.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    269
    Quote Originally Posted by Matticus View Post
    You can use exclusive-OR with a mask to toggle a bit.

    You can roll this into a macro, and add bit-shifting to create the mask for the desired position.

    Code:
    #define BIT_FLIP(x,y)  /* fill in the blank; 'x' is the binary pattern, 'y' is the bit position */
    Okay, I don't really see any code here.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    I was leaving the coding up to you. Brewbuck gave you the general algorithm.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    269
    Here is what I've done:

    Code:
    uint64_t flip(uint64_t num, int p) {
        if (num & (1 << p)) {
            //odd
            num &= ~(1 << p);
            return num;
        }
        num  ^= (1 << p);
        return num;
    }
    For some reason, people seem to only want to give algorithms and not actual code for this problem.
    Last edited by dayalsoap; 02-19-2015 at 04:07 PM. Reason: mistake in code

  7. #7
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,127
    Quote Originally Posted by dayalsoap View Post
    For some reason, people seem to only want to give algorithms and not actual code for this problem.
    We don't provide code as answers to questions as many or most of the questions involve homework assignments. It is the policy here and with most web sites to help instruct the OP with enough information so the OP can code the answer themselves.

    How can anyone learn HOW to program, if we do the coding for them?

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Is it working for you?

    I'm not sure what you're doing with the "if", but you should be (almost) fine with just the last two lines of that function (hint: Since your mask starts at '1', you need to shift it one less than time than you think).

    Quote Originally Posted by dayalsoap View Post
    For some reason, people seem to only want to give algorithms and not actual code for this problem.
    If you're not good enough at coding to know how to do it, you generally won't get code since the point is for you to figure it out yourself and learn in the process.

    If you are good enough at coding to know how to do it, you generally won't get code since you could do it yourself.

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    269
    Quote Originally Posted by Matticus View Post
    Is it working for you?

    I'm not sure what you're doing with the "if", but you should be (almost) fine with just the last two lines of that function (hint: Since your mask starts at '1', you need to shift it one less than time than you think).



    If you're not good enough at coding to know how to do it, you generally won't get code since the point is for you to figure it out yourself and learn in the process.

    If you are good enough at coding to know how to do it, you generally won't get code since you could do it yourself.
    This line

    Code:
    num  ^= (1 << p);
    Hmm. You are right. No need for the if. I thought this always sets it to 0, but it actually just flips it.
    Last edited by dayalsoap; 02-19-2015 at 04:33 PM.

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    This is where a pen-and-paper analysis would help you understand the details.

    Code:
    /*
    num = 1101  // binary
    p   = 3     // position
    
    >> num  ^= (1 << p);
    
    Right side:
    
      0001 << 3 - 1  // minus one because we start at 1, so we shift one less time
    
      0010 // shift one
      0100 // shift two ... now our set bit is in the third position
    
    Now we do the XOR:
    
      1101  // num
      0100  // mask
      ----
      1001  // result
     
    
    A zero in the mask will not change the value:
      0 xor 0 = 0
      0 xor 1 = 1
    
    A one in the mask will change the value:
      1 xor 0 = 1
      1 xor 1 = 0
    */
    See how I subtracted one of the position variable? And do you understand why?

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    269
    Quote Originally Posted by Matticus View Post
    This is where a pen-and-paper analysis would help you understand the details.

    Code:
    /*
    num = 1101  // binary
    p   = 3     // position
    
    >> num  ^= (1 << p);
    
    Right side:
    
      0001 << 3 - 1  // minus one because we start at 1, so we shift one less time
    
      0010 // shift one
      0100 // shift two ... now our set bit is in the third position
    
    Now we do the XOR:
    
      1101  // num
      0100  // mask
      ----
      1001  // result
     
    
    A zero in the mask will not change the value:
      0 xor 0 = 0
      0 xor 1 = 1
    
    A one in the mask will change the value:
      1 xor 0 = 1
      1 xor 1 = 0
    */
    See how I subtracted one of the position variable? And do you understand why?
    I don't see why I need to subtract one, at all. If I pass in position 0, I don't want to subtract position -1.

    You can run the code as I've written with your removal of the if statement and it runs exactly fine.

  12. #12
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    For some reason, I assumed you were considering the LSB as position 1.

    Apart from that, did the analysis I showed help you understand the mechanics of the algorithm?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. flipping bool values
    By pjb in forum C Programming
    Replies: 2
    Last Post: 04-11-2012, 10:56 AM
  2. Flipping a coin!
    By Valdo in forum C++ Programming
    Replies: 2
    Last Post: 03-26-2012, 11:04 AM
  3. coin flipping
    By MiroMage in forum C Programming
    Replies: 5
    Last Post: 10-01-2008, 06:47 PM
  4. Page flipping
    By JoshG in forum Game Programming
    Replies: 8
    Last Post: 06-04-2002, 07:05 AM
  5. Screen Flipping
    By Bemoi in forum C++ Programming
    Replies: 13
    Last Post: 04-03-2002, 04:56 PM