views:

68

answers:

2

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 ?

A: 

You could do

pow(2, i) ^ n
Colin
you'd have to cast the (inexact) result of pow to an int for that to work.
Mark E
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
A: 

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

Stephen Canon