Pages: [1] 2   Go Down
Author Topic: Need help understanding a line of code...  (Read 1220 times)
0 Members and 1 Guest are viewing this topic.
Salt Lake City
Offline Offline
Jr. Member
**
Karma: 1
Posts: 69
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
  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!
Logged

California
Online Online
Faraday Member
**
Karma: 92
Posts: 3447
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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:
if ((yOff % 4) == 0)
  leap = 1;
else
  leap = 0;
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 227
Posts: 14024
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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

Rob Tillaart

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

Saskatchewan
Offline Offline
Sr. Member
****
Karma: 19
Posts: 364
When the going gets weird, the weird turn pro. - Hunter S. Thompson
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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?
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
leap = ((yOff % 4) == 0);
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Salt Lake City
Offline Offline
Jr. Member
**
Karma: 1
Posts: 69
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Fort Lauderdale, FL
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6144
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Sometimes bad programmers write code only they think is clever.
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.c

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
leap = !(yOff & 3);
Logged

Worst state in America
Offline Offline
God Member
*****
Karma: 32
Posts: 819
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Gentlemen may prefer Blondes, but Real Men prefer Redheads!

Worst state in America
Offline Offline
God Member
*****
Karma: 32
Posts: 819
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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)"
Logged

Gentlemen may prefer Blondes, but Real Men prefer Redheads!

Salt Lake City
Offline Offline
Jr. Member
**
Karma: 1
Posts: 69
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 310
Posts: 26626
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

"triadic operator"
Logged

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

Worst state in America
Offline Offline
God Member
*****
Karma: 32
Posts: 819
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Gentlemen may prefer Blondes, but Real Men prefer Redheads!

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 310
Posts: 26626
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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)
Logged

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

Worst state in America
Offline Offline
God Member
*****
Karma: 32
Posts: 819
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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?

Logged

Gentlemen may prefer Blondes, but Real Men prefer Redheads!

Pages: [1] 2   Go Up
Jump to: