Java - Bitwise AND operator &

What is Bitwise AND operator?

The bitwise AND & operator returns 1 if both bits are 1, and 0 otherwise.

Consider the following code,

int i = 13 & 3;

Java int uses 32 bits in 8-bit chunks in memory.

13 is 00000000 00000000 00000000 00001101

3 is 00000000 00000000 00000000 00000011

13 & 3

becomes

00000000 00000000 00000000 00001101
00000000 00000000 00000000 00000011
-----------------------------------
00000000 00000000 00000000 00000001

Therefore, 13 & 3 is 1.

Demo

public class Main {
  public static void main(String[] args) {
    int i = 13 & 3;
    System.out.println("i = " + i);
  }/*from www  .j av  a  2  s  .  co  m*/
}

Result