Site icon TestingDocs.com

Java Bitwise OR operator

Overview

In this tutorial, we will learn about Java Bitwise OR operator with a simple java program. Bitwise OR operator produces 1 if one or both of the corresponding bits in its operands are 1, and produces 0 if both of the corresponding bits are 0. The (|) is the bitwise OR operation symbol.

Table

The outcomes table with the bitwise OR operator is shown as follows:

Operand: A Operand: B Bitwise OR: A | B
0 0 0
0 1 1
1 0 1
1 1 1

The OR operator returns one in all cases except when the bits of both operands are zero.

Sample Program

Sample demo program to illustrate the operator usage.

/* 
 * Java Program to demonstrate 
 * Bitwise OR Operator.
 * Java Tutorials -- www.TestingDocs.com 
 */
public class BitwiseORDemo {
    public static void main(String args[])
     {
    	  //Declare two variables
          int A = 1;
          int B = 0; 
          System.out.println("A | B =: " + (A | B));
          A = 0;
          B = 0;
          System.out.println("A | B =: " + (A | B));
     }
}

Sample output

A | B =: 1
A | B =: 0

 

The above program displays the two rows of the outcome table. Modify the A and B values to check the other rows of the outcome table for the OR operator.

Java Tutorial on this website:

https://www.testingdocs.com/java-tutorial/

For more information on Java, visit the official website :

https://www.oracle.com/java/

Exit mobile version