I seem to remember that there's a hardware solution using a couple of exclusive OR gates and a counter.....