While there is not a difference in the code produced by the compiler, bit masks are traditionally written using the hexadecimal notation, because it's significantly easier for a human to convert to a binary form. Another common convention is to include the leading zeros when the length of the field is known. E.g. for a C int
field, it's common to write:
#define MASK 0x0000ffff
In addition, hexadecimal constants indicate to the programmer that it's probably a bit mask, or a value that will be somehow involved in bitwise operations and should probably be treated specially.
As a bonus, hexadecimal notations may also avoid issues with negative numbers: 0xffffffff
is in fact a negative number (-1
to be exact). Rather than juggling with the sign and 2's-complement numbers you can just specify the mask in hexadecimal and be done with it.
Since Java 7 you can also use binary literals which makes it even easier for a human to understand which bits are set in a bit mask. And binary literals may make use of underscores to put the bits into separate groups.
That means that the following is also valid:
public final static int DELETION_MASK = 0b0000_0001;
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…