سؤال

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 ;)

هل كانت مفيدة؟

المحلول

Result[i] = someMath*(bits&1) + someMath*((bits&2)>>1)
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top