Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 981
thuclh Avatar asked Dec 27 '22 03:12

thuclh


2 Answers

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.

like image 93
Peter Lawrey Avatar answered Jan 11 '23 23:01

Peter Lawrey


Java Operators

Bitwise Operations

<< is the left bit shift operator.

like image 37
Jeffrey Avatar answered Jan 12 '23 01:01

Jeffrey