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

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

 채택된 답변

Wayne King
Wayne King 2013년 6월 15일
편집: Wayne King 2013년 6월 15일

0 개 추천

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

xor(A,B) is true if one of A and B is true, else false
Wayne King
Wayne King 2013년 6월 15일
편집: Wayne King 2013년 6월 15일
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)
Yes,
not( [ xor(1,0), xor(0,1), xor(1,1), xor(0,0) ] )
returns
ans =
0 0 1 1
J
J 2013년 6월 16일
Many thanks. Couldn't wrap my head around the latter part of it.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Roger Stafford
Roger Stafford 2013년 6월 16일

0 개 추천

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)
Andrei Bobrov
Andrei Bobrov 2013년 6월 16일

0 개 추천

~rem(year,4)&rem(year,100)|~rem(year,400)

카테고리

도움말 센터File Exchange에서 Calendar에 대해 자세히 알아보기

질문:

J
J
2013년 6월 15일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by