Java - Bitwise OR Operator |

What is Bitwise OR Operator?

The bitwise OR | operates returns 1 if either bit is 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 00001111

The value of 13 | 3 can be computed as follows. The result of 13 | 3 is 15.

Demo

public class Main {
  public static void main(String[] args) {
    int i = 13 | 3;
    System.out.println("i = " + i);
  }//from  w  ww .  ja  v a2  s. c  o m
}

Result