Saturday, June 17, 2017
Logical and bit wise operators in java
Logical and bit wise operators in java
In java both logical and bitwise operators are used. Logical operator returns the true / false value depends on the condition check and bitwise return the same true / false depends on the condition check but the difference between these operator is that logical operator only work with the boolean value where as bitwise operator work with both boolean and numeral values. How that will we see later in this post.
Logical operator
Logical operator in java only deals with the boolean value that means it will return either true or false depending on the condition check. If we try to use any other data type then it will give compilation error.
There are three types of logical operator i.e
1> Logical AND ( && )
2> Logical OR ( | | )
3> Logical NOT ( ! )
Logical AND
It is represented with the double ampersand ( && ) sign. basically it is a binary operator and both the operand must return either true or false value and no others data type are not acceptable with this operator.
Operand 1 Operand 2 Result
True True True
True False False
False True False
False False False
Conclusion: If any of the operand is False then it will return False.
Now think is there any need of checking if 1st operand is false ?
I think No because it is obvious that it will return false only but if 1st operand is true that means it may true or false so it is mandatory to check 2nd operand as well.
Note :- Compiler does the same that if 1st operand is false then no need to check further it further and hence save some compilation time.
for ex-
if ( false && true && true && true ) // It will check the 1st operand if it is false then its obvious that whole statement will lead to false only so compiler will not check further.
You can have any statement in place of true and false here but which returns either true or false value.
Logical OR
It is a binary operator and both the operand must return either true or false value and no others data type are not acceptable with this operator. It is represented like this ( | | ).
Operand 1 Operand 2 Result
True True True
True False True
False True True
False False False
Conclusion: If any of the operand is True then it will return False.
Now think is there any need of checking if 1st operand is true?
I think No because it is obvious that it will return True only but if 1st operand is false then there may be chance of returning true or false so it will be mandatory for compiler to check 2nd operand as well.
Note :- Compiler does the same thing that if 1st operand is true then no need to check it further and hence save some compilation time.
if ( true | | false | | true | | false ) // It will check the 1st operand if it is true then its obvious that whole statement will lead to true only so compiler will not check further.
Here you can have any statement in place of true and false but it should return either true or false value.
Logical NOT
It is a unary operator and it will always return the result as reverse of its operand. Like if operand is true then it will return false and vice-versa.
Operand 1 Result
True False
False True
Conclusion: If the operand is True then it will return False and vice-versa.
for ex-
if ( ! true ) // return false
if ( ! false ) // return true
Bitwise Operator
It is same as Logical operator but it can operate both with boolean and numeral value. There are four types of bitwise operator i.e
1> Bitwise AND ( & )
2> Bitwise OR ( | )
3> Exclusive OR ( ^ )
4> Bitwise NOT ( ~ )
Why named as bitwise ?
It is because for numeral values it deals bit by bit. How it deals bit by bit we will see later in this post.
Bitwise AND
It is a binary operator and represented with a single ampersand ( & ).It works same as logical operator in case of boolean value but how it works with numeral value is bit interesting .
How Bitwise AND works with the numeral value?
It deals with each bit that means it will result as 0 always and 1 when both the value are 0.Now think in zeros and ones. We all know that zero represent always false and one represent true always.
Here it is also the same story that compiler will compare bit by bit and return the result accordingly.
for ex-
System.out.println( (1 & 2 ) ) ; // It will display Zero as output.
1-> 01
2-> 10
---------
00 ( 0 )
---------
Bitwise OR
It is a binary operator.It works same as logical operator in case of boolean value but how it works with numeral value is bit interesting . It is represented like this ( | ).
How Bitwise OR works with the numeral value?
It is same as Bitwise AND but only difference is it will result as 1 always and 0 when both the value are 0.
System.out.println( (1 | 2 ) ) ; // It will display Three ( 3 ) as output.
1-> 01
2-> 10
---------
11 ( 3 )
---------
Exclusive OR
It is a binary operator and represented with ( ^ ) this symbol. It result false when both the operand are either true or false otherwise true So compiler cannot be sure about result unless it checks 2nd operand.
Operand 1 Operand 2 Result
True True False
True False True
False True True
False False False
Conclusion : It will return False if both the are operand are have same value either true or false otherwise true. It is same in case of bits also if both the bits are same either 1 or 0 then it will return 0 otherwise 1.
Have fun with code guys. keep experimenting!
Available link for download