i need have integer counter have values 1 6 inclusive in wrap around fashion. namely starting 1, count 6 , go 1. wonder if it's possible achieve using sort of bitwise logic. of course it's trivial if statement below wondering 1 liner :)
int counter = 1; for... if (counter++ == 7) counter = 1;
addition: here why want 1 liner:
byte output = (byte)((inputbyte & 0xf8) | counter++);
use modulo operator.
do something; ++counter; counter %= 7; ++counter;
to use real bitwise operations modulo must power-of-2 not case here.
ps: if counter started @ 0 instead of 1, wouldn't need second ++counter
, write :
for (int counter = 0; (somecondition); ++counter, counter %= 7) { something; }
pps: note if
performs faster modulo, tho.
Comments
Post a Comment