Need help understanding a line of code...

Hi all,

Can anyone help me understand what is going on in this line of code? I've never seen this kind of syntax before.

leap = yOff % 4 == 0;

Both "leap" and "yOff" are of uint8_t type. As far as I can tell, if yOff divided by four has no remainder, leap will equal one. The line is located in the following code block:

  for (yOff = 0; ;++yOff){
      leap = yOff % 4 == 0;
      if (days < 365 + leap)
          break;
      days -= 365 + leap;
  }

This code isn't mine, I'm simply trying to understand what is happening in Adafruit's RTC library (GitHub - adafruit/RTClib: A fork of Jeelab's fantastic RTC Arduino library).

Thanks in advance for any and all help!

leap = yOff % 4 == 0;

Take yOff and modulus divide 4. If the result is 0, assign 1 to leap. Otherwise, assign 0 to leap.

A functionally equivalent example is

if ((yOff % 4) == 0)
  leap = 1;
else
  leap = 0;

In plain English "if year is dividable by four => leap year is true"

leap = yOff % 4 == 0;

Here's how I see this statement. I'm doing this mostly to see if I'm understanding it or not.

First we have a variable name followed by the assignment operator. This will assign the value to the variable leap whatever the right side expresses to. The comparison operator is the key on the right side as leap will get assigned the results of the comparison. Which will be 1 if yOff is divisible by 4 and 0 otherwise.

So how did I do?

Personally I'd prefer to have that code with the precedence stated explicitly, just to avoid each person looking at it having to check:

leap = ((yOff % 4) == 0);

Thank you all for your help, my guess was correct. Personally, I have never seen anyone write code like that!

Sometimes bad programmers write code only they think is clever.

I think that's pretty clear. "It's a leap year if the year modulo 4 is 0." The order of operations also makes sense in this case: math is before logic which is before assignment.

This would probably be "cleverer" although I'm not sure how much optimization takes place

leap = !(yOff & 3);

madvoid:
Hi all,

Can anyone help me understand what is going on in this line of code? I've never seen this kind of syntax before.

leap = yOff % 4 == 0;

That code says "The boolean (true/false) value of "leap" is equal to whatever "yOff modulo 4 == 0" evaluates to.

yOff modulo 4 means that yOff is looked at as a repeating sequence of 0, 1, 2 and 3. For example 0 == 0, 1 == 1, etc... 4 == 0, 5 == 1, etc. After you divide yOff by 4 as many times as you can, the REMAINDER is the result.

So, imagine that yOff was equal to "2000". 2000 modulo 4 is 0. Since "0 == 0" is true, the variable "leap" would be set to "true".

If yOff was equal to "2011", the modulo result would be 3. 3 is not equal to 0, to "leap" would be set to "false".

The whole line simply tells you "If year is a leap year, then "leap" equals "true", otherwise "leap" equals false".

Hope this helps.

madvoid:
Thank you all for your help, my guess was correct. Personally, I have never seen anyone write code like that!

Another crazy way to write that would be:

leap = (yOff % 4) ? 0 : 1;

The ? and : operators are very handy. They simply work like this:

"IF X is TRUE then ? (THIS HAPPENS) otherwise : (THIS HAPPENS)"

Krupski:
Another crazy way to write that would be:

leap = (yOff % 4) ? 0 : 1;

The ? and : operators are very handy. They simply work like this:

"IF X is TRUE then ? (THIS HAPPENS) otherwise : (THIS HAPPENS)"

I'm very glad you told me that! The code actually uses this sytax later on and I was wondering what it did. Do the question mark and colon operators have any special names so i can look them up?

"triadic operator"

AWOL:
"triadic operator"

...which means nothing to a person who doesn't know what it is.

And even if they have heard of it, trying to understand how it works can be tricky unless it's explained well... and I've seen a lot of textbooks that do a terrible job of making the reader UNDERSTAND the concept.

It's like college electronics and other engineering classes. The professor usually scribbles all kinds of cryptic looking equations on the board, the student scrambles to copy them into his notes, then the prof. asks "any questions?" and the student is so baffled by it all that he can't even think of WHAT to ask. OK, good, on to the next equation.....

I prefer to teach with diagrams, analogies and simple explanations FIRST and get the student to understand the concept, THEN present the math. That way, the student already understands and says "Ah ha! I see".

Imagine teaching a grade school kid "Y=MX+B". Call it a "first order linear equation". HUH? Talk about the slope and intercept. HUH? What is it even used for???

But, instead FIRST give some real world examples like "degrees C to degrees F conversion" and show how at 0 degrees C the F value is 32 and therefore there is an offset. Then show how degrees F climb up "faster" than degrees C (by drawing a little letter F climbing up a hill and a little letter C climbing up a shallower hill).

NOW, introduce "slope" (the hill) and "intercept" (the offset) and bingo! The light of understanding shines brightly.

There's nothing worse than a teacher that can't teach.

There's nothing worse than a teacher that can't teach.

Oh come now, really there are lots of things worse.
Degenerative brain disease, sociopaths with assault weapons... the list is nearly endless.

...which means nothing to a person who doesn't know what it is.

Which is why I put it in quotes so that the OP could cut and paste and answer their own question

so i can look them up?

then come back here if there was anything they didn't understand.

(I teach SCUBA diving. All my students have survived, so I consider myself quite good at it)

madvoid:
I'm very glad you told me that! The code actually uses this sytax later on and I was wondering what it did. Do the question mark and colon operators have any special names so i can look them up?

Yes indeed. The question mark denotes the thing that happens if "X" is true and the colon denotes the thing that happens if "X" is false.

Here's an example (pseudo code):

what_is_larger = (moon < sun) ? "The sun" : "The moon"

The test condition is "moon less than sun". Since the moon is smaller than the sun, "(moon < sun)" evaluates to "TRUE". Therefore, the string after the QUESTION MARK is the one that is placed into the variable "what_is_larger".

I could also write it this way:

what_is_larger = (moon > sun) ? "The moon" : "The sun"

Since, in this case, "(moon > sun)" is FALSE, the string after the COLON is placed into the variable "what_is_larger".

Got it?

AWOL:

There's nothing worse than a teacher that can't teach.

Oh come now, really there are lots of things worse.
Degenerative brain disease, sociopaths with assault weapons... the list is nearly endless.

...which means nothing to a person who doesn't know what it is.

Which is why I put it in quotes so that the OP could cut and paste and answer their own question

so i can look them up?

then come back here if there was anything they didn't understand.

(I teach SCUBA diving. All my students have survived, so I consider myself quite good at it)

I agree with you especially on the nutcases that shoot up schools. I'm a gun owner and people who do such terrible things make my hobby all the more difficult to enjoy (because all gun owners get lumped into the "psycho murderer" category).

Krupski:
Yes indeed. The question mark denotes the thing that happens if "X" is true and the colon denotes the thing that happens if "X" is false.

Here's an example (pseudo code):

what_is_larger = (moon < sun) ? "The sun" : "The moon"

The test condition is "moon less than sun". Since the moon is smaller than the sun, "(moon < sun)" evaluates to "TRUE". Therefore, the string after the QUESTION MARK is the one that is placed into the variable "what_is_larger".

I could also write it this way:

what_is_larger = (moon > sun) ? "The moon" : "The sun"

Since, in this case, "(moon > sun)" is FALSE, the string after the COLON is placed into the variable "what_is_larger".

Got it?

@Krupkski,@AWOL,

Thank you for your help, it makes perfect sense. The above explanation was very helpful. Is there any reason (other than succinctness) to use this as opposed to an if-else statement?

I suppose the main reason is you can embed it in expressions, but you can always code around it and a decent compiler will probably generate the same code.