This article was originally published on JRoller on the 9/9/2011
Quite often, you would see lines like these in a program, for defining some bit masks:
Don't pay too much attention to the constants name, imagine something more meaningful here. The point is, when the mask is encompassing more and more bits, you end up with cryptic numbers, like this 2097152 at the end. Often, the developer would add a comment, like this:
Recently (and that's the reason for my post), I spotted this attempt at giving a meaning to these numbers:
Although mathematically correct, it seemed to me a bit overkill for the low order bits. Also, using a general mathematical method for bit calculation shows that the developer has not much idea about the existing bit operations existing natively in Java. So if you really want to give a meaning to those numbers, so the next developer would understand, I suggest rather using a bit shifting operation, like this: