Question

I want to use 2bits to switch on and off parts of a mathematical statement in a loop. Kinda like:

Result[i] = someMath*bits[0] + someMath*bits[1]

(bits[n] refers to index n, not a value of n)

Using them as flags but then in each loop I then want to increment them more as a binary encoded value:

bits++

So with each iteration the 2bits will cycle through 00,01,10,11,00,01 ...

Searching I found bitfields but I don't see an increment or array like way to access the elements.

Any ideas appreciated.

If it has to be 8bits, that's fine, the lowest two bits will still follow the pattern I'm after ;)

Was it helpful?

Solution

Result[i] = someMath*(bits&1) + someMath*((bits&2)>>1)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top