When
n++ & 1
executes, lets expect n
is odd number at the moment, condition is true.
Post incrementation is applied and n
is even when
result += n
executes. Thats why its counting even numbers instead of odd. Modify code to
while(n < 99) result += !(n++ & 1) ? n : 0;
and it will count odd one’s.
2
solved C++ random bitwise behavior