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.

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
13  
Why does this smell like a homework question? –  John Bode Feb 24 '12 at 14:46
1  
It does smell like a homework question, doesn't it? –  mcfinnigan Feb 24 '12 at 14:48
3  
Smells like homework when it uses "suppose" and "the result of the program". –  Bernard Feb 24 '12 at 15:00
    
It probably is homework, but please don't add that tag - meta discussion –  ChrisF Feb 24 '12 at 15:29
    
Sorry, I did not realize that tag was set to be removed. –  jmq Feb 24 '12 at 17:58
show 2 more comments

2 Answers

up vote 4 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
add comment

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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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