Thread: Date representation into DOS/Window

  1. #1
    Registered User
    Join Date
    Mar 2019
    Posts
    51

    Date representation into DOS/Window

    Formula:

    512 * (year - 1980) +32 * month + day

    Why do we multiply 512 with year?
    and why do we multiply 32 with month?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    They're packed as bits in a 16-bit integer
    yyyyyyymmmmddddd

    There are at most 31 days. The next highest power of 2 is 32, which gives the multiplier of 32 for month.

    There are 12 months. The next highest power of 2 is 16, which gives the multiplier of 512 (aka 32*16) for year.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Because DOS uses a 16 bits encoding:
    Code:
    Bit: 111111
         5432109 8765   43210
         year    month  day
    day from 0x01 (1) to 0x1f (31) -- 5 bits
    month from 0x01 (1) to 0x0c (12) - 4 bits:
    year: remaining 7 bits.

  4. #4
    Registered User
    Join Date
    Mar 2019
    Posts
    51
    Quote Originally Posted by Salem View Post
    They're packed as bits in a 16-bit integer
    yyyyyyymmmmddddd
    They are packed in 32 bits according to my textbook.
    How does it will work for 32 bits?

  5. #5
    Registered User
    Join Date
    Mar 2019
    Posts
    51
    Quote Originally Posted by rm82co View Post
    They are packed in 32 bits according to my textbook.
    How does it will work for 32 bits?
    512*(year-1980)+32*month+day is equivalent to (year-1980) << 9 | month << 5 | day

    512 and 32 are powers-of-two. multiplying by them is the direct equivalent of a bitwise "shift left" operation. 1980 is simply this date system's "epoch".

    I think it is like this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Program that output the very next date after Input a date
    By rumel.ehsan in forum C Programming
    Replies: 7
    Last Post: 03-23-2013, 11:53 AM
  2. Replies: 2
    Last Post: 05-07-2012, 07:37 AM
  3. Replies: 10
    Last Post: 03-28-2012, 10:30 AM
  4. Replies: 11
    Last Post: 03-27-2012, 11:37 PM
  5. Two date objects showing same date?
    By dxfoo in forum C++ Programming
    Replies: 7
    Last Post: 06-17-2010, 06:06 PM

Tags for this Thread