(If you plan to use binary comparisons.)
I updated the position of a ball in a state-driven update loop. The ball’s position should update only if we are in SERVE or RETURN state.
if ( (gameState & (SERVE|RETURN)) != 0) ball.move(bounds); // bounds is a Rectangle of our world
Initially, I counted the states by 1, 2, …
int START = 1; int ... = 2; int ... = 3; int SERVE = 4; int RETURN = 5;
That non-power-of-2 five messes up the logic.
00001 & (00100 | 00101) = 00001 & (00101) = 00101 00101 != 00000 TRUE
The body of the conditional occurs. The reference to Ball ball hasn’t been initialized yet, so we get a NullPointerException. If we use numeric states strictly in the powers of 2, we only get one 1s digit and the rest are zero. Then we can use this idea of “bit-masking.” Renumerating SERVE and RETURN as 8 and 16 gives us 001000 and 010000, respectively.
00001 & (01000 | 10000) = 00001 & (11000) = 00000
With this change, the conditional doesn’t evaluate to true until game state is either SERVE or RETURN specifically.