Operators with
precedence 5,6 and 7
|
Precedence |
Operator |
Type |
Associativity |
|
5 |
| |
Bitwise OR |
Left to Right |
|
6 |
^ |
Bitwise EX-OR |
Left to Right |
|
7 |
& |
Bitwise AND |
Left to Right |
Bitwise OR
This operation performs
bitwise OR operation of the input numbers. An ‘OR’ gate outputs 1 whenever any
one of its input is 1 and it outputs 0 only when all the inputs are 0. Consider
the OR implementation of two numbers 7 and 1. Here bitwise operation is
performed.
Output is ‘0’ only
when both the input is 0, else 1.
Java program to implement Bitwise OR operation
class
operator{
public static void main(String
args[]){
//Assigning 2 numbers
byte num_1=7;
byte num_2=1;
//Performing bitwise OR operation
System.out.println("Bitwise
OR of 7 and 1 is " + (num_1 | num_2));
// '+' means concatenation
operation
}
}

Java Implementation of Bitwise EX-OR operation
In EX-OR, the output is 0, when all the inputs are same, the output is 0, whereas if the inputs are different, the output is 1. This can be clearly understood by the following Java code stating 2 cases.
class operator{
public static void main(String args[]){
//Case 1:
// Assigning 2 same inputs
byte num_1=0;
byte num_2=0;
//Case 2:
// Assigning different inputs
byte num_3=0;
byte num_4=1;
//Performing bitwise EX-OR operation
System.out.println("Bitwise EX-OR result of case 1 is " + (num_1 ^ num_2));
System.out.println("Bitwise EX-OR result of case 2 is " + (num_3 ^ num_4));
// '+' means concatenation operation for printing both the statement and output
}
}
In case 1, the 2
inputs are same and so the output is 0. In case 2, different inputs are given
and hence the output is 1.
Java
Implementation of Bitwise AND operation
An AND gate outputs 1, only when all of its input is 1 else it prints out 0. This concept can be better understood by a simple Java code.
class operator{
public static void main(String args[]){
//Assigning 4 numbers
byte num_1=1;
byte num_2=1;
byte num_3=0;
byte num_4=1;
//Performing bitwise AND operation
System.out.println("Bitwise AND of all 4 numbers is " + (num_1 & num_2 & num_3 &num_4));
// '+' means concatenation operation for printing both the statement and output
}
}Output
Here, all the
numbers expect num_3 is 1 and so the output is 0.
To learn more about Java operators, you can check these useful links:




1 Comments
Nice
ReplyDelete