0

I am trying to understand the code used to determine if a year is a leap year, I am stuck with this part

(year & 3 || !(year % 25) && year & 15 ? 28 : 29)

I don't get what year & 3 or year & 15 means. I know in some programming languages the ampersand sign is used for concetanation, I don't think that is the case here.

I know what && means, just not sure what a single ampersand on it own means.

I have read a bit about the leap year test regarding numbers being divisible by 4,100 and 400. But I can't find anything abohut & 3 and & 15.

VLAZ
  • 26,331
  • 9
  • 49
  • 67
Ross Symonds
  • 690
  • 1
  • 8
  • 29
  • 1
    It's bitwise AND. It's an obfuscated way to test for multiples of 4 and 16. – Barmar Sep 29 '21 at 19:55
  • 4
    [Is there a & logical operator in Javascript](https://stackoverflow.com/q/3374811) | [What do these JavaScript bitwise operators do?](https://stackoverflow.com/q/4535328) | [What does this symbol mean in JavaScript?](https://stackoverflow.com/q/9549780) – VLAZ Sep 29 '21 at 19:56
  • 2
    This code seems to be deliberately intended to be confusing, or maybe it's Code Golf so they're going for the shortest formula. – Barmar Sep 29 '21 at 19:57
  • Shouldn't these be answers? I think the question is fully answered by these comments. – ᴓᴓᴓ Sep 29 '21 at 20:05
  • Barmar this the whole test - daysPerMonth = month == 4 || month == 6 || month == 9 || month == 11 ? 30 : (month == 2 ? (year & 3 || !(year % 25) && year & 15 ? 28 : 29) : 31); – Ross Symonds Sep 29 '21 at 20:06
  • 00000000011 - 3 11111010000 - 2000 So in the year 2000 would 'year & 3' give you 00000000000? – Ross Symonds Sep 29 '21 at 20:07

0 Answers0