Java Bitwise OR

Java Bitwise OR Operator is used to perform OR operation between the respective bits of given operands.

Syntax

The syntax for Bitwise OR operation between x and y operands is

x | y

The operands can be of type int or char. Bitwise OR operator returns a value of type same as that of the given operands.

The following table illustrates the output of OR operation between two bits.

bit1bit2bit1 | bit2
000
011
101
111
ADVERTISEMENT

Examples

In the following example, we take integer values in x and y, and find the bitwise OR operation between x and y.

Main.java

public class Main {
    public static void main(String[] args) {
        int x = 5;
        int y = 9;
        //Bitwise OR
        int result = x | y;
        System.out.println("Result : " + result);
    }
}

Output

Result : 13

In the following example, we take char values in x and y, and find the bitwise OR operation between x and y.

Main.java

public class Main {
    public static void main(String[] args) {
        char x = 'a';
        char y = 'b';
        //Bitwise OR
        int result = x | y;
        System.out.println("Result : " + result);
    }
}

Output

Result : 99

Conclusion

In this Java Tutorial, we learned what Bitwise OR Operator is, its syntax, and how to use this operator in Java programs, with the help of examples.