Reputation: 7550
Is the result of a boolean operation guaranteed to be 1 or 0?
I'm programming a microcontroller and need to set a pin to 1 or 0, and was wondering if I can shorten the code from this:
isRunning = isStarted && !isSleeping;
_PBH0 = isRunning ? 1 : 0;
to this:
isRunning = isStarted && !isSleeping;
_PBH0 = isRunning;
Upvotes: 2
Views: 712
Reputation: 122383
Yes, the result of &&
operator (or any of the logical operators) is an int
that has a value of either 1
or 0
.
C11 §6.5.13 Logical AND operator
The
&&
operator shall yield1
if both of its operands compare unequal to0
; otherwise, it yields0
. The result has typeint
.
Upvotes: 8