Ad Code

Responsive Advertisement

BITWISE OPERATORS

 

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

As the name of each operator suggests, these operators are used in implementing bitwise operations. Let’s see the functioning of each operator with a simple Java code. To learn about operators with precedence 1 to 4, CHECK HERE

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
                }
            }

 Output

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
                }
            }

 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.





PREVIOUS

                                                                                                                                    NEXT

To learn more about Java operators, you can check these useful links:

ASSIGNMENT OPERATORS

LOGICAL OPERATORS

BITWISE OPERATORS

RELATIONAL OPERATORS

SHIFT OPERATORS

  ARITHMETIC OPERATORS

UNARY OPERATORS


Post a Comment

1 Comments