Generating Alternating 0 and 1

Once upon a time, a friend of mine gave some of us a challenge. It's not exactly the same but quite similar ;) Anyway, think of ways you can generate alternating 0 and 1. If you have an Update() loop, the first time you call update it will generate 0 and the next time will be 1, and then 0,1,0,1,.. you get the idea. I found 3 ways to do this (assuming initial value of i is 0).

Math + Bit
i = (i+1) & 1;      // You might think of this i = (i+1) % 2.
Simple Math
i = 1 - i;
Bits Operations
i = i ^ 1;
Can you come up with more ways?

Comments

Popular posts from this blog

GDC 2015 Links

D3D11 Compute Shader - Part 1

Mapping Square Texture to Trapezoid / Quadrilateral