Вопрос

In Programming in the Key of C#, the author gives an example (with source code) on how a date (year, month, day -- in numbers) can be packed in a 32 bit integer. In the example, the author packs the information like so:

int iDate = (iYear << 9) | (iMonth << 5) | iDay;

If I'm grasping this properly, a left shift of 9 gives us just a value of 512 (or 1023 if counting every bit position). However, I notice when running the program values like 2014 (year) can be stored. How is this possible with such a small value? Am I misunderstanding a piece of this code? No amount of reading, watching or toying with the code has helped me clear the air.

Это было полезно?

Решение

The shift doesn't represent the max value that the year can take (even though you made a mistake in your calculation, that's not the reason we shift 9 on the left). The shift lets you define ranges which will be used for each value. Below is an ascii that shows how the values will be organized in an Int32 after the values are shifted

11111111111111111111111 1111 11111
-----------yyyyyyyyyyyY mmmM ddddD

Since a day value can be at most 31, 5 bits will suffice (32 possible values). For the month, we will need 4 bits (16 possible values) which will accomodate our 12 possibilities. The year needs at least 12 bits if we want our smart date scheme to continue after 2048. We still have some room on the left to last for a long time.

So that is why we shift the month value 5 times, and the year value 9 times. Also that's why an Int16 is not enough, we need more bits than 16 to store our date

Другие советы

A left shift by 9 might give 512, if you started with 1. However, what I think is going on here, is that assume that the lowest 9 bits are used to store the number, when in fact the lower 9 bits are not used to store that value. Shifting left by 9 moves the "used bits" over to the left by 9, freeing up the 9 lowest bits. In that piece of 9 bits, you can only store 512 different values (0 though 511), but that's not the piece that you're using for the years.

To put it as a picture, it ends up like this, assuming the values don't "overlap" (they shouldn't, unless you suddenly have 16 or more months, or 32 or more days)

syyy yyyy yyyy yyyy yyyy yyym mmmd dddd

Plenty of room for the years for the foreseeable future.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top