All Those Binary Dates

Only a handful of days of the year have a number format containing only ones and zeros. These “binary dates” are found by examining each day of the year to check for binary digit validity. In last week’s Lesson, I presented code to generate and save each of the year’s 365 dates as a 4-character string. It’s time to check each of them for binary date validity.

The update to the code includes a function, binary(), which examines a 4-character string to ensure that it contains only characters ‘1’ and ‘0’. The function returns the date’s value if it’s binary, -1 otherwise. This value is examined in the main() function. If the result is something other than -1, the date and its hexadecimal value are output.

Here is the code:

2025_11_15-Lesson.c

#include <stdio.h>

int binary(char *v)
{
    int b,x,digit;

    b = 0;
    for( x=0; x<4; x++ )
    {
        b<<=1;
        digit = *v-'0';
        if( digit==0 || digit==1 )
            b+=digit;
        else
            return(-1);
        v++;
    }

    return(b);
}

int main()
{
    int month,d,b;
    int days[12] = {
        31, 28, 31, 30, 31, 30,
        31, 31, 30, 31, 30, 31
    };
    char date[5];

    for( month=0; month<12; month++ )
    {
        for( d=0; d<days[month]; d++ )
        {
            snprintf(date,5,"%02d%02d",
                    month+1,
                    d+1
                  );
            b = binary(date);
            if( b != -1 )
                printf("%s = %X\n",date,b);
        }
    }

    return 0;
}

The binary() function examines each of the four characters in string v. Variable b holds the result, which is initialized to zero. This value is shifted left one bit for each iteration of the for loop, which sets the bits in the proper order and builds the binary value: b<<=1;

The value of variable digit is set by subtracting '0' from the character in *v. This result is then compared with zero or one. If true, the digit (0 or 1) is added to variable b. Otherwise, the instant an invalid character is encountered the function returns -1.

After the if test, pointer v is incremented to reference the next character in the string. If the loop finishes without returning early, the proper binary value in variable b is returned.

In the main() function, the value returned from binary() is tested for -1. If not, the date string is output along with its value in hexadecimal:

0101 = 5
0110 = 6
0111 = 7
1001 = 9
1010 = A
1011 = B
1101 = D
1110 = E
1111 = F

Fifteen days ago, the date was 1101, 0xD. Other dates are shown above. The reason you don’t see all values from 0000 through 1111 is that no month is recorded as double-zero by humans; January is 01 not 00. Likewise, the first day of the month is 01 and not 00, which explains why the gamut of dates goes from January 10th through November 11th and includes only nine values.

I started this blog in 2013. Had I started it in 2011, the range of dates could have also included the current year, as in 110111. That would be a fun exercise. Alas, the next “binary” year value is in 2100. Perhaps some AI controlled robot in the future can code an update to this exercise on January 1st of that year?

Leave a Reply