While looking through some example C code, I came across this:
y -= m < 3;
What does this do? It it some kind of condensed for loop or something? It's impossible to google for as far as I know.
While looking through some example C code, I came across this:
y -= m < 3;
What does this do? It it some kind of condensed for loop or something? It's impossible to google for as far as I know.
m < 3
is either 1
or 0
, depending on the truth value.
So y=y-1
when m<3
is true
, or y=y-0
when m>=3
If you break it down by order of precedence for each operation, you get:
y = (y - (m < 3));
m < 3
gets evaluated and returns a boolean result 1
or 0
, so the expression can be simplified as
y = y - 1; // if m < 3 is true
or
y = y - 0; // if m < 3 is false
The purpose for doing this is to avoid an if
clause.
I means if (m < 3) { y -=1; }
, since (m < 3)
is 1 if m is less than 3, 0 otherwise.
The code appears in some hoary old reference implementation of something to do with either leap years or Easter, or possibly both: the first two months January and February are special because they occur before the leap day. There isn't really any excuse for writing code like that, unless you actually like the look of it. Most people don't.
m < 3
evaluates to 1 if m is less than 3. Hence, y is decreased by one in this case. Thus, an if statement avoided.
Most interesting is the amount of code it creates. On a simple Arduino system (compiler version 1.8.10) both the original and the
y -= (m < 3 ? 1 : 0);
create the same code size. However, the 'if' version:
if(m<3) {y -= 1;}
actually creates 8 bytes less code (4 instructions less). A good example of how creating clever code doesn't necessarily result in smaller code, especially with a good compiler.
I can't tell you want it's for, but I can tell you what it does:
m < 3
returns an int of 0 or 1 representing a boolean value.
if m is less than 3, the statement evalutates as: y -= 1
or y = y - 1
;
if m is greater than or equal to 3, the statement evalutates as y -= 0
or y = y - 0
. Overall in this case, the statement does nothing.
I agree that the code is equivalent to if(m<3) y=y-1;
However it is unsafe code because while FALSE is always 0, TRUE has had a couple of definitions. Nowadays TRUE is equal to 1 in most programming systems, but in older systems it was equal to -1 (where all bits where set to on, not just the last one).
So if this code is running on an older platform it could translate to if(m<3) y=y-(-1);
or if(m<3) y=y+1;
Cross system compatibility is a big reason to avoid 'clever' code like this.