Advantage of using 0x01 instead of 1 for an integer variable?

后端 未结 4 1857
我在风中等你
我在风中等你 2020-12-16 12:44

Recently I came across a line like this

public final static int DELETION_MASK = 0x01;

why is it not like

4条回答
  •  猫巷女王i
    2020-12-16 13:16

    It helps with the mental conversion between the integer value and the bit pattern it represents, which is the thing that matters for flags and masks.

    Because 16 is a power of 2 (unlike 10), you get nice repeating things like this:

    public final static int A_FLAG = 0x01;  // 00000001
    public final static int B_FLAG = 0x02;  // 00000010
    public final static int C_FLAG = 0x04;  // 00000100
    public final static int D_FLAG = 0x08;  // 00001000
    public final static int E_FLAG = 0x10;  // 00010000
    public final static int F_FLAG = 0x20;  // 00100000
    public final static int G_FLAG = 0x40;  // 01000000
    public final static int H_FLAG = 0x80;  // 10000000
    

提交回复
热议问题