Bit write question

hi !

i have one question…

int BITMASK_A = 0x1 << 0; // means 1 right ?
int BITMASK_B = 0x1 << 1; // means 2 right ? 

why developers just write as below ?

 int BITMASK_A = 1;
 int BITMASK_B = 2;

OR

   int BITMASK_A = 0x1 ; 
    int BITMASK_B = 0x2; 

WHY using ’ << ’ so often so that the codes looks more complicated ??

It’s just a good pratice. In this case there’s just 1 and 2, but imagine 3 more (C, D and E):

int BITMASK_A = 0x1 << 0;
int BITMASK_B = 0x1 << 1;
int BITMASK_C = 0x1 << 2;
int BITMASK_D = 0x1 << 3;
int BITMASK_E = 0x1 << 4;

or is this easier?:

int BITMASK_A = 1;
int BITMASK_B = 2;
int BITMASK_C = 4;
int BITMASK_D = 8;
int BITMASK_E = 16;

Well that’s a matter of personal opinion :slight_smile:

1 Like

This is just for convenience, as @makalele was pointing out.

The shifting operations are really fast and are convenient for bit masking.
Left shift is a multiplication by the power of two, and right shift is a division by the power of two. These operations are way faster than the their equivalent * or / operations.

1 Like