Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

What does this line of code mean in non-code speak?

Asked by J on 15 Jun 2013
if (div4 & ~( xor(div100, div400)))  

div4 div100 and div400 are given by:

div4 = ((year/4) == floor (year/4));
div100 = ((year/100) == floor (year/100));
div400 = ((year/400) == floor (year/400));

0 Comments

J

Products

No products are associated with this question.

3 Answers

Answer by Wayne King on 15 Jun 2013
Edited by Wayne King on 15 Jun 2013
Accepted answer

div4, div100, and div400 are all logical variables, 1 or 0.

 if (div4 & ~(xor(div100,div400)))

says "if div4 is true (1) and div100 and div400 are both false or both are true, do something"

~xor(div100,div400)

equals 1 (true) only if both div100 and div400 are false or both are true

4 Comments

Wayne King on 15 Jun 2013

right, ~xor(A,B) is true only if both are false or both are true (I forgot the both are true condition)

~xor(1,0)
~xor(0,1)
~xor(1,1)
~xor(0,0)
per isakson on 15 Jun 2013

Yes,

    not( [ xor(1,0), xor(0,1), xor(1,1), xor(0,0) ] )

returns

    ans =
         0     0     1     1
J on 16 Jun 2013

Many thanks. Couldn't wrap my head around the latter part of it.

Wayne King
Answer by Roger Stafford on 16 Jun 2013

In other words, this logical statement is true when 'year' is to be a leap year under the Gregorian calendar. They could just as well have written

 if div4&(div100==div400)

or, given the definitions of these quantities,

 if div4&(div100<=div400)

or, again given their definitions, even this

 if div400|(div4~=div100)

0 Comments

Roger Stafford
Answer by Andrei Bobrov on 16 Jun 2013
~rem(year,4)&rem(year,100)|~rem(year,400)

0 Comments

Andrei Bobrov

Contact us