Last active
July 2, 2020 16:57
-
-
Save rindeal/8ed6fdb24650c9d17416 to your computer and use it in GitHub Desktop.
IsLeapYear()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdbool.h> | |
#include <assert.h> | |
/** | |
* Modified version of https://stackoverflow.com/a/11595914/2566213 | |
* with 4000th check added. | |
*/ | |
bool | |
IsLeapYear(const int y) | |
{ | |
return ( | |
((y & 3) == 0) && | |
( | |
(y % 25 != 0 ) || | |
( | |
((y & 15) == 0) && | |
(y % 4000 != 0) | |
) | |
) | |
); | |
} | |
void | |
AssertIsLeapYear() | |
{ | |
assert(!IsLeapYear(1901)); | |
assert(!IsLeapYear(1903)); | |
assert(!IsLeapYear(1905)); | |
assert(IsLeapYear(1904)); | |
assert(IsLeapYear(1908)); | |
assert(IsLeapYear(1996)); | |
assert(IsLeapYear(2004)); | |
assert(!IsLeapYear(1700)); | |
assert(!IsLeapYear(1800)); | |
assert(!IsLeapYear(1900)); | |
assert(!IsLeapYear(2100)); | |
assert(IsLeapYear(1600)); | |
assert(IsLeapYear(2000)); | |
assert(IsLeapYear(2400)); | |
assert(IsLeapYear(3600)); | |
assert(IsLeapYear(4400)); | |
assert(!IsLeapYear(4000)); | |
assert(!IsLeapYear(8000)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment