3 ms·
This one is easier to understand imho: 30+(1&(5546>>M)) (also except for M=2)
by pasodoble 15y ago
This one is easier to understand imho: 30+(1&(5546>>M))
(also except for M=2)
- dxbydt 15y agoWell done! Could you elaborate on how you came up with 5546 ? Perhaps there's a simpler technique than the one I describe below. btw, this was the technique that I learnt in school - 1. You have 12 months. Ignore February. Then you have 11. 2. The remaining 11 map to either 30 days or 31 days . ie. 30+0, or 30+1. 3. So the object is to write a many-to-one function that maps some members of the set to 0, rest to 1. Add 30 and you get the right answer ie. number of days in that month. 4. So you want to map {1,3,5,7,8,10,12} to 1, and {4,6,9,11} to 0. 5. Write down a 13 bit number. If you rightshift x times and AND it with unity, you get 1. 6. So then, x belongs to {1,3,5,7,8,10,12}. 7. That means the bit in each of those locations must be on. The rest of the bits must be off. That gives you the number 1010110101010, which is simply 5546 in decimal. 8. So that's ((5546>>M)&1)+30, except for M==2.