Join the Stack Overflow Community
Stack Overflow is a community of 6.5 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

Possible Duplicate:
How do you set, clear and toggle a single bit in C?

Can some one help me how to toggle a bit at ith position. One way is to ((n>>i)^1) < < i. Are there any other ways ?

share|improve this question

marked as duplicate by James McNellis, bernie, Mark Elliot, Stephen Canon, Alok Singhal Sep 10 '10 at 1:20

This question was marked as an exact duplicate of an existing question.

1  
up vote 0 down vote accepted

n ^= 1U << i is easy enough, isn't it?

share|improve this answer

You could do

pow(2, i) ^ n
share|improve this answer
1  
you'd have to cast the (inexact) result of pow to an int for that to work. – Mark Elliot Sep 10 '10 at 1:15
    
No, you couldn't. pow returns a double, which is not a valid argument to the ^ operator. There's also no guarantee that pow will give an exact result. – Stephen Canon Sep 10 '10 at 1:16

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