What is this expression in Java (1 << 2)?

I don't know what this means "1 <2" in:

public static final int MODIFY_METADATA = 1 << 2; // modify object

      

Please help me!

+3


source share


2 answers


Java Operators

Bitwise operations



<<

- left bit shift operator.

+4


source


If you want to know why use use 1 << 2

and not 4, which is the same value, it is because you explicitly want to use a bitmask, eg.

public static final int FLAG0 = 1 << 0;
public static final int FLAG1 = 1 << 1;
public static final int MODIFY_METADATA = 1 << 2;

      



Indicates that each value is in a bit mask.

+3


source







All Articles