What does y -= m < 3 mean?

C

C Problem Overview


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.

C Solutions


Solution 1 - C

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

Solution 2 - C

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.

Solution 3 - C

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.

Solution 4 - C

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.

Solution 5 - C

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.

Solution 6 - C

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.

Solution 7 - C

< has a higher priority to the sign of equal = so m<3 returns 1 if m is less than 3, and else is 0

then y = y - 1 or y = y - 0

Solution 8 - C

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTheTedinatorView Question on Stackoverflow
Solution 1 - CWei ShiView Answer on Stackoverflow
Solution 2 - CJasarienView Answer on Stackoverflow
Solution 3 - CSteve JessopView Answer on Stackoverflow
Solution 4 - CAttishOculusView Answer on Stackoverflow
Solution 5 - CJames WebsterView Answer on Stackoverflow
Solution 6 - CThomas CravenView Answer on Stackoverflow
Solution 7 - CDanView Answer on Stackoverflow
Solution 8 - CMark BrownView Answer on Stackoverflow