[Tutor] toggle idiom?

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Wed, 8 May 2002 18:40:04 -0700 (PDT)


On Tue, 7 May 2002, Lindsay Davies wrote:

> Yes, this will do it for you...
>
> a = a ^ 1
>
> Lindsay


Alternatively:

###
a = 1 - a
###

is something that can toggle between zero and one.


Another one-liner could be:

###
a = (a + 1) % 2
###


although that one might be a bit more expensive than the other method.


So there are a lots of good choices here.  Good luck!