std::cout<<"odd" is an expression that std::cout will return (which is why you can do std::cout << a << b << c ). When evaluated in a boolean context, it simply returns true if the failure bit is not set. Therefore, if the output operation completes successfully, it will be evaluated as true.
However, the purpose of this code is not to check this value, rather it is a smart (and not very readable) method 1 :
if (remainder & 1) { std::cout << "odd"; } else { std::cout << "even"; }
It exploits the short-circuited nature of the && and || :
- In
a && b , if a is false, then it is evaluated as a ( b not evaluated!), Otherwise it is evaluated as b . - In
a || b a || b , if a true, then it is evaluated as a ( b not evaluated!), otherwise it is evaluated as b .
So, if remainder & 1 evaluates to false (in this case, zero), then std::cout << "odd" not evaluated, because the expression && short circuit that returns false. This is the left operand of the external expression || , which causes an evaluation of its b ( std::cout << "even" ), writing "even" to the output.
If remainder & 1 evaluates to true (in this case, nonzero), then the right operand for && evaluates to an odd mapping. Assuming this operation succeeds, the left operand for the operation || will be true, which will lead to its short circuit and will not evaluate the right operand.
1 Experienced programmers probably know exactly what is happening here, but, as you understand, this method is not the most readable. It is better (IMO) to understand the intent of the code, so I would just use a conditional if - or at least use the ternary operator: std::cout << (remainder & 1 ? "odd" : "even") .
In other languages ββ(JavaScript comes to mind) (ab) using short-circuited operators is a very common method. Usually I donβt see them using this method in C ++, and I would strongly discourage such use.