The Bits
Please Log In for full access to the web site.
Note that this link will take you to an external site (https://shimmer.mit.edu) to authenticate, and then you will be redirected back to this page.
Bits as Days
Suppose we'd like to represent whether or not any given day in a year is a holiday or not. One solution would be to make an array of integers, with one integer for each day, and store a 1 or a 0. This solution would be a huge waste of space since the "thing" we need to represent only needs one-bit to do so. Instead, it would be a more efficient usage of memory to use actual individual bits.
Assume you have an array of 12 32-bit integers representing the twelve months of the year. The integers are each used as 32-bit holders indicating whether or not the day of that month is a holiday (starting with the lsb for Day 1 of a month). Note this means the last bit of every int is unused (there are no 32-day months) and for some other months, the 31st, 30th, and 29th bits may also not be used.
For the questions below you may use decimal, binary (prepend with 0b), or hexadecimal (prepend with 0x) in your solutions.
Suppose you wanted to determine if a particular day in a given month was a holiday. To do this, you would first create a bitmask which is an integer where all the bits are set to zero except for the one that represents the day that you are interested in.
Due to some historical events, the government in Arstotzka decided that the 11th day of every month is now going to be a holiday regardless of whether or not it already is a holiday. Assume you have a single month represented with the int month.
The government in Arstotzka has increased the chocolate ration from 32 oz to 9 oz! Thank goodness. However, as a result, the first six days of every month must be work days regardless of any holidays that existed previously. For a given month month, update it so that the first six days are now workdays, however, make sure to leave other holidays in place!
month, updates it so that the first six days are now workdays, and leaves all other holidays in place. Don't forget your semicolon!
Ok, now with this in mind, let's write a function called holiday_count that takes in an array of months, months, that is 12 long (one for each month). Tally up the total number of holidays in a year and return it.