Go Down

Topic: Need help understanding a line of code... (Read 1 time) previous topic - next topic

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.
Code: [Select]
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:
Code: [Select]
  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 (https://github.com/adafruit/RTClib).

Thanks in advance for any and all help!

Arrch

Code: [Select]
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

Code: [Select]
if ((yOff % 4) == 0)
  leap = 1;
else
  leap = 0;

robtillaart


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

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Jimmy60

Code: [Select]
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?

PeterH

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

Code: [Select]

leap = ((yOff % 4) == 0);
I only provide help via the forum - please do not contact me for private consultancy.

madvoid

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

James C4S

Sometimes bad programmers write code only they think is clever.
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

WizenedEE


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
Code: [Select]
leap = !(yOff & 3);

Krupski


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.
Code: [Select]
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.
Gentlemen may prefer Blondes, but Real Men prefer Redheads!

Krupski


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:

[font=monospace]leap = (yOff % 4) ? 0 : 1;[/font]

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

[font=monospace]"IF X is TRUE then ? (THIS HAPPENS) otherwise : (THIS HAPPENS)"[/font]
Gentlemen may prefer Blondes, but Real Men prefer Redheads!

madvoid


Another crazy way to write that would be:

[font=monospace]leap = (yOff % 4) ? 0 : 1;[/font]

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

[font=monospace]"IF X is TRUE then ? (THIS HAPPENS) otherwise : (THIS HAPPENS)"[/font]


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?

AWOL

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Krupski


"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.
Gentlemen may prefer Blondes, but Real Men prefer Redheads!

AWOL

Quote
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.

Quote
...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
Quote
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)
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Krupski


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?

Gentlemen may prefer Blondes, but Real Men prefer Redheads!

Go Up