In this section you will learn about how to use" |" operator in java. Java provides six types of operators: among them one is Bitwise (OR) "|" operator which can be applied to integer, long, int char short type. Bitwise OR "|" operator work on bit and perform bit by bit operation.
Java bitwise OR "|" operator
In this section you will learn about how to use" |" operator in java. Java provides six types of operators: among them one is Bitwise (OR) "|" operator which can be applied to integer, long, int char short type. Bitwise OR "|" operator work on bit and perform bit by bit operation.
Example: Let us suppose a = 5 and b = 6, Now in binary format they will be as follows:
a = 0000 0101
b = 0000 0110
a | b = 0000 0111
"|" operator work on bit , so it will produces 1 if any one of the operand will be 1 or both will be 1. But if both bit are 0 (0|0) it will produces 0. In more simple word , if both bit are 0 then it will produce 0 but if one of the bit is 1 then it will produce 1.
Code here displays the use of "|" operator in Java: A simple example using Bitwise (OR) "|" operator.
public class Operators{ public static void main(String args[]) { int a=5,b=6,c=0; c=a|b; // use of "|" operator System.out.println("Result of A | b = "+c); } }
In the above example, "a "is 5 that is 0000 0101 and "b" is 0000 0110. So the last most bit in a is 1 and the last most bit in b is 0, So it will produces 1 because one of the bit is 1. and similarly it will work on bit by bit.
Output: After compiling and executing the above program.