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

19,599

Solution 1

Java Operators

Bitwise Operations

<< is the left bit shift operator.

Solution 2

If you want to know why would use use 1 << 2 rather than 4 which is the same value, it because you explicitly want to be using a bit mask e.g.

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

Shows each value is in a bit mask.

Share:
19,599
thuclh
Author by

thuclh

Working at Sống Chung

Updated on August 18, 2022

Comments

  • thuclh
    thuclh about 1 year

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

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

    Please help me!