Code to do a "bit iterator" but with 3 states
Question: I am wanting to do a counter that iterates over 3 states. I know how to do
this for 2 states using the bit operator `i^=1`.

I want to know if there is a way to do similiar but with three states?

I realize I can just do:

    
    
    i = 0
    while
       if(i==3)
           i = 0
       do stuff here
       i++
    

But I was hoping for something more elegant and efficient. I was thinking of a
modulus trick or something.

(Right now I am using python)


Answer: Could
[itertools](https://docs.python.org/2/library/itertools.html#itertools.cycle
"itertools") be useful for you?

    
    
    import itertools
    states = itertools.cycle([0, 1, 2])
    while True:
      i = states.next()
      <do stuff>
    

