Go Down

### Topic: Need help understanding a line of code... (Read 2666 times)previous topic - next topic

##### Dec 17, 2012, 08:26 pm
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

#1
##### Dec 17, 2012, 08:39 pm
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

#2
##### Dec 17, 2012, 08:44 pm

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

#3
##### Dec 17, 2012, 09:40 pm
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

#4
##### Dec 18, 2012, 12:27 am
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);`

#5
##### Dec 18, 2012, 12:37 am
Thank you all for your help, my guess was correct.  Personally, I have never seen anyone write code like that!

#### cmiyc

#6
##### Dec 18, 2012, 02:11 am
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

#7
##### Dec 18, 2012, 06:47 am

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

#8
##### Dec 18, 2012, 10:14 am

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

#9
##### Dec 18, 2012, 10:22 am

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!

#10
##### Dec 18, 2012, 10:25 am

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

#11
##### Dec 18, 2012, 10:27 am
"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.
I speak for myself, not Arduino.

#### krupski

#12
##### Dec 18, 2012, 10:39 am

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

#13
##### Dec 18, 2012, 10:46 am
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.
I speak for myself, not Arduino.

#### krupski

#14
##### Dec 18, 2012, 10:49 am

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