Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

This question already has an answer here:

Suppose you were given an integer number in decimal notation. This when represented in binary will be a series of 0's and 1's. This sequence varies in length with the magnitude of the number. Now suppose you wanted to flip the 'n'th position bit in this sequence of binary numbers, how would you do it?

The result of the program must be the number represented by the sequence with the bit in the 'n'th position flipped.

Edit: This is not a homework question! An intern asked me this question! And all I could do was fumble! So could someone give me a helping hand here! All I can think of is assembly level stuff right now (put it in a register and blah, blah)! But is there a way to do it in C? I have been out of school for quite some time now! Trust me, my only homework nowadays is probably tax returns! :-D

share|improve this question

marked as duplicate by gnat, GlenH7, MichaelT, Wayne M, BЈовић Jul 7 at 6:46

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
What purpose would doing this have? Not asking OP asking generally? When would this be an appropriate thing to do? –  Ominus Feb 24 '12 at 21:03

2 Answers 2

up vote 5 down vote accepted

If you are assuming you are implementing C, you can apply a mask to the variable.

For example:

a    = 0xF2  // Binary: 11110010
b    = 0xF6  // Binary: 11110110
mask = 0x04  // Binary: 00000100

c = a ^ mask  // Binary: 11110110  -> the 3rd LSB made 0 to 1 
d = b ^ mask  // Binary: 11110010  -> the 3rd LSB made 1 to 0  

Basically, X'Or with 0 keeps the bit as it is. X'OR with 1 flips the bit.

Refer to this: http://en.wikipedia.org/wiki/Bitwise_operations_in_C

share|improve this answer
1  
I usually define mask values so it's clear which bit is being set. Like int mask = (1<<(n)) to set the nth bit. –  TMN Feb 24 '12 at 16:10
    
Hi Dipan. This is a case-by-case basis solution. I mean to say the mask value would be determined on the basis of the number and the position of the bit we are trying to flip rather than the position of the bit to be flipped. Isn't there a more general way to go about doing this? –  Arpith Feb 27 '12 at 13:55

A bit can be turned on by applying bitwise OR with a power of two to the operand. It is turned off by applying bitwise AND with the complement of a power of two to the operand

Which power to use and how to distinguish 'on' from 'off' flips is left as an exercise for the reader.

share|improve this answer
    
Kilian, what you said in your post is a simplified, beat-down, slightly distorting version of Dipan's answer and also from some textbook. My question for a generic "flipping" of the bit means that I do not know the initial state of the bit to see if it is to be "turned on" or "turned off". Whether I use AND or OR would depend on the number I'm working with (again a case by case basis). –  Arpith Mar 16 '12 at 8:01

Not the answer you're looking for? Browse other questions tagged or ask your own question.