Well, for the flag changing at every odd number, it looks like your boolean should be true if and only if (n+3)%4 <= 1
, where %
is the modulo operator:
n (n+3)%4 Boolean
-- ------- -------
0 3 false
1 0 true
2 1 true
3 2 false
4 3 false
5 0 true
6 1 true
7 2 false
8 3 false
9 0 true
10 1 true
11 2 false
12 3 false
:: : : :
I've deliberately added three instead of subtracting one, since some languages have different ideas of what the modulo operator should do for negative numbers. And keep in mind that this is language-agnostic. If you're specifically looking for a C or C-like language solution, see the excellent answer here from Christoffer Hammarström ((n + 1) & 2
just in case it ever disappears) - this is far more succinct in those particular languages.