Observations:
Java has a logical AND operator.
Java has a logical OR operator.
Java has a logical NOT operator.
Problem:
Java has no logical XOR operator, according to sun. I would like to define one.
Method Definition:
As a method it is simply defined as follows:
public static boolean logicalXOR(boolean x, boolean y) {
return ( ( x || y ) && ! ( x && y ) );
}
Method Call:
This method is called in the following way:
boolean myVal = logicalXOR(x, y);
Operator Usage:
I would much rather have an operator, used as follows:
boolean myVal = x ^^ y;
Question:
I can't find anything on how to go about defining a new operator in Java. Where should I start?
Java does have a logical XOR operator, it is ^ (as in a ^ b).
Apart from that, you can't define new operators in Java.
Edit: Here's an example:
public static void main(String[] args) {
boolean[] all = { false, true };
for (boolean a : all) {
for (boolean b: all) {
boolean c = a ^ b;
System.out.println(a + " ^ " + b + " = " + c);
}
}
}
Output:
false ^ false = false
false ^ true = true
true ^ false = true
true ^ true = false
Isn't it x != y ?
Java has a logical AND operator.
Java has a logical OR operator.
Wrong.
Java has
two logical AND operators: normal AND is & and short-circuit AND is &&, and
two logical OR operators: normal OR is | and short-circuit OR is ||.
XOR exists only as ^, because short-circuit evaluation is not possible.
Perhaps you misunderstood the difference between & and &&, | and ||
The purpose of the shortcut operators && and || is that the value of the first operand can determine the result and so the second operand doesn't need to be evaluated.
This is especially useful if the second operand would results in an error.
e.g.
if (set == null || set.isEmpty())
// or
if (list != null && list.size() > 0)
However with XOR, you always have to evaluate the second operand to get the result so the only meaningful operation is ^.
You can just write (a!=b)
This would work the same as way as a ^ b.
Logical exclusive-or in Java is called !=. You can also use ^ if you want to confuse your friends.
That's because operator overloading is something they specifically left out of the language deliberately. They "cheated" a bit with string concatenation, but beyond that, such functionality doesn't exist.
(disclaimer: I haven't worked with the last 2 major releases of java, so if it's in now, I'll be very surprised)
The following your code:
public static boolean logicalXOR(boolean x, boolean y) {
return ( ( x || y ) && ! ( x && y ) );
}
is superfluous.
Why not to write:
public static boolean logicalXOR(boolean x, boolean y) {
return x != y;
}
?
Also, as javashlook said, there already is ^ operator.
!= and ^ work identically* for boolean operands (your case), but differently for integer operands.
* Notes:
1. They work identically for boolean (primitive type), but not Boolean (object type) operands. As Boolean (object type) values can have value null. And != will return false or true when one or both of its operands are null, while ^ will throw NullPointerException in this case.
2. Although they work identically, they have different precedence, e.g. when used with &: a & b != c & d will be treated as a & (b != c) & d, while a & b ^ c & d will be treated as (a & b) ^ (c & d) (offtopic: ouch, C-style precedence table sucks).
The only operator overloading in Java is + on Strings (JLS 15.18.1 String Concatenation Operator +).
The community has been divided in 3 for years, 1/3 doesn't want it, 1/3 want it, and 1/3 doesn't care.
You can use unicode to create method names that are symbols... so if you have a symbol you want to use you could do myVal = x.$(y); where $ is the symbol and x is not a primitive... but that is going to be dodgy in some editors and is limiting since you cannot do it on a primitive.
Here is a var arg XOR method for java...
public static boolean XOR(boolean... args) {
boolean r = false;
for (boolean b : args) {
r = r ^ b;
}
return r;
}
Enjoy
You can use Xtend (Infix Operators and Operator Overloading) to overload operators and 'stay' on Java
What you're asking for wouldn't make much sense. Unless I'm incorrect you're suggesting that you want to use XOR to perform Logical operations the same way AND and OR do. Your provided code actually shows what I'm reffering to:
public static boolean logicalXOR(boolean x, boolean y) {
return ( ( x || y ) && ! ( x && y ) );
}
Your function has boolean inputs, and when bitwise XOR is used on booleans the result is the same as the code you've provided. In other words, bitwise XOR is already efficient when comparing individual bits(booleans) or comparing the individual bits in larger values. To put this into context, in terms of binary values any non-zero value is TRUE and only ZERO is false.
So for XOR to be applied the same way Logical AND is applied, you would either only use binary values with just one bit(giving the same result and efficiency) or the binary value would have to be evaluated as a whole instead of per bit. In other words the expression ( 010 ^^ 110 ) = FALSE instead of ( 010 ^^ 110 ) = 100. This would remove most of the semantic meaning from the operation, and represents a logical test you shouldn't be using anyway.
I am using the very popular class "org.apache.commons.lang.BooleanUtils"
This method is tested by many users and safe. Have fun.
Usage:
boolean result =BooleanUtils.xor(new boolean[]{true,false});
A and B would have to be boolean values to make != the same as xor so that the truth table would look the same. You could also use !(A==B) lol.
This is an example of using XOR(^), from this answer
byte[] array_1 = new byte[] { 1, 0, 1, 0, 1, 1 };
byte[] array_2 = new byte[] { 1, 0, 0, 1, 0, 1 };
byte[] array_3 = new byte[6];
int i = 0;
for (byte b : array_1)
array_3[i] = b ^ array_2[i++];
Output
0 0 1 1 1 0
Because boolean data type is stored like an integer, bit operator ^ functions like a XOR operation if used with boolean values.
//©Mfpl - XOR_Test.java
public class XOR_Test {
public static void main (String args[]) {
boolean a,b;
a=false; b=false;
System.out.println("a=false; b=false; -> " + (a^b));
a=false; b=true;
System.out.println("a=false; b=true; -> " + (a^b));
a=true; b=false;
System.out.println("a=true; b=false; -> " + (a^b));
a=true; b=true;
System.out.println("a=true; b=true; -> " + (a^b));
/* output of this program:
a=false; b=false; -> false
a=false; b=true; -> true
a=true; b=false; -> true
a=true; b=true; -> false
*/
}
}
Here's an example:
Given 2 int values, return true if one is negative and one is positive. Except if the parameter "negative" is true, then return true only if both are negative.
public boolean posNeg(int a, int b, boolean negative) {
if(!negative){
return (a>0 && b<0)^(b>0 && a<0);
}
else return (a<0 && b<0);
}
you'll need to switch to Scala to implement your own operators
pipe example
Can be achieved using stream API in java 8 and above
public static boolean logicalXOR(boolean x, boolean y) { // can modify to take [] or list of bools
return Stream.of(x, y) // modify as per method params
.filter(bool -> bool)
.count() == 1;
}
Related
I have run this code in compiler and getting the output as 1 but I don't understand how it will be 1. Please explain with answer.
public class Main {
public static void main(String args[]) {
int a = 10;
int b = 2;
System.out.println((a < b) ? a++ : --b);
}
}
The ternary operator ?: will check the condition (a < b), since it is false, it will execute the expression after :, which is --b.
--b will subtract b by 1, and return the value after subtraction, which is 1.
The ternary operator is equivalent to a function that returns a value based on a condition. So let's write that function:
int ternary(int a, int b) {
if(a < b) {
int temp = a;
temp = temp + 1;
return a;
} else {
b = b - 1;
return b;
}
}
int a = 10;
int b = 2;
System.out.println((a < b) ? a++ : --b);
In the above code (a < b) is checking if a is less than b and if its true a++ will be executed, and if it is false (which is the case in this situation) --b will be executed, and as the value of b is initialized as 2 it will equal to 1 after doing --b operation
because as per the turnery operator in java
Expression1 ? Expression2: Expression3;
If Expression1 is evaluated at true, Expression2 will be executed otherwise Expression3
There are several great answers already. This took a bit to edit for formatting, but hopefully useful to illustrate what's happening.
The interesting part of your program is in the println() statement:
System.out.println((a < b) ? a++ : --b)
^^^^^^^^^^^^^^^^^^^
The whole thing inside the () is an expression with several things going on. At a high level, there is a boolean condition (a < b) followed by two other expressions: first is a++, second is --b. Below is a breakdown.
(a < b) ? a++ : --b
----- --- ---
^ ^ ^
| | |__ second expression, used if condition is "false"
| |
| |__ first expression, used if condition is "true"
|
|__ this is the boolean condition to check; whatever is inside must return either true or false
This is how the entire expression is evaluated:
Is the boolean condtion true or false? To answer that, we need to know: what does a < b evaluate to? Since a = 10, and b = 2, we can substitute the values (10 for a, 2 for b). So the question becomes: what does 10 < 2 evaluate to? Since 10 is not less than 2, the result of the boolean condition is false.
Which of the of two expressions should we go to? Since the result from #1 was false, we skip the first expression (a++), and go to the second one (--b).
What is --b? That's a fancy way of saying: b = b - 1 and also use the new value of b as the result of the expression. Since b = 2, it's equivalent to: b = 2 - 1, and use the new b value (1) as the result of the expression. So this expression evaluates to "1", and that's what is printed out. This part is tricky though. A variation would be b-- instead of --b – if you did that, it would mean: set the new value of b to the result of "2 - 1" (so, new value is "1" like with --b), but: for the expression result, use the old value. So --b ends up printing "1" in your example, and b-- would end up printing "2".
The following code results in false. Could anyone explain to me why? I am having some trouble understanding using truth tables. I am a beginner with java, thank you in advance for the help.
boolean x = false;
boolean y = true;
boolean z = true;
System.out.println((!x || y && !z));
I expected the printed line to be false but the correct answer is true.
The factors that come into play here are operator precedence, left-to-right evaluation, and the special "short circuit" nature of '&&' and '||'.
'and' binds tighter than 'or'.
Firstly, operator precedence means the expression is effectively
( (!x) || (y && !z) );
Given the short-circuit execution of '||', once we know that !x is true, the right-hand side does not get evaluated.
I wrote this hack to print out each term as it was evaluated:
static boolean P(String s, boolean x) { System.out.println(" "+s); return x; }
and then replaced every boolean term 'b' to a call to P("b", b):
{
... same as before...
System.out.println( P("!x",!x) || (P("y",y) && P("!z",!z)));
}
results in output
!x
true
note: nothing related to y or z printed.
please help me i cant figure out how does the operators(AND, NOT, XOR ,,..ETC) work in java. I know the output of AND and OR but i am clueless at NOT. For example i don't completely understand statement such as a variable != integer(i!= 3). i mean how does the NOT operator work.for example how does NOT work here.
class Demo {
public static void main(String args[]) throws java.io.IOException {
char ch;
do {
System.out.print("Press a key followed by ENTER: ");
ch = (char) System.in.read(); // get a char
} while (ch != 'q');
}
}
if you make some System outs you will figure out:
char ch = 'l';
System.out.print(2 != 3);
--true, they are not equal
System.out.print('q' != 'q');
-- false, they are equals
System.out.print(ch != 'q');
-- true, they are not equals
it means, they != checks if they are exactly the same (be careful in this case is used for primitive types, such as int, char, bool, etc. this operator work differently in objects, such as String)
int x = 4;
int y = 5;
!case a means not case a
x == y means x and y are referencing the same place in memory (to understand what that means, see this question).
x != y is the opposite of the above, and is the same as writing !(x == y) (not x equals y)
case a && case b And operator - Both case a and case b are true.
case a || case b Or operator - Either case a or case b are true.
Here are same examples so everything is clearer:
1==1 // true, as 1 is equal to 1
2==1 // false, as 1 is not equal to 2.
!true // false, as not true is false.
1!=1 // false, as 1 is equal to one.
!(1==1) // The same as above - exact same output.
// You need the brackets, because otherwise it will throw an error:
!1==1 // Error - what's '!1'? Java is parsed left-to-right.
true && false // false - as not all cases are true (the second one is false)
true || false // rue - as at least one of the cases are true (the first one)
The ! operator is unary. When applied to Boolean values it switches, for example
bool on = true;
System.out.print(!on); //false
System.out.print(on); //true
When used next to an equal sign, it checks if the values are not equal. If they are not equal, it returns true. Otherwise, it returns false. For example,
int two = 2;
int three = 3;
System.out.print(two != three);
//returns true, since they are not equal
System.out.print(two == three);
//returns false, since they are not equal
This question already has answers here:
Why do we usually use || over |? What is the difference?
(28 answers)
Closed 9 months ago.
Can any one explain the difference between and usage of OR operator ( || and | ) in java. thanks
e.g:
if(a || b) {
// Do something.
}
and
if(a | b) {
// Do something.
}
This is simple. http://www.roseindia.net/help/java/o/java-operator.shtml says:
OR operator is a kind of a conditional operators, which is represented
by | symbol. It returns either true or false value based on the state
of the variables i.e. the operations using conditional operators are
performed between the two boolean expressions.
The OR operator (|) is similar to the Conditional-OR operator (||) and
returns true, if one or another of its operand is true.
Note: In || operator if have more than one condition and if first condition return true then other conditions ignored but in | operator all condition examin.
There are more information on http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html
The first one is a logical or. Both sides of the operator are handled as boolean values and it results in a boolean. In case the variables in question are not boolean themselves they become false if they are 0 or null.
The second one is a bit-wise or operator. This one usually only works with integer numbers. I compares the two values bit by bit and gives the resulting number. For example:
5 | 6 = 7 (decimal)
101 | 110 = 111 (binary)
For further details have a look at Wikipedia: Logical disjunction
If you use the operator || JVM will not bother to evaluate the right-hand operand alone.
the || operator is a boolean operator
it can be interpreted in simple english as...
if ( a is true or b is true)
{
//do soemthing
}
the | operator is a logical operator
it works only on integral types like int, char etc...
it is the bitwise OR operation on the two operands
example:
bool a = true;
bool b = false;
bool c = a | b;
//c will be true
if(a | b )
{
}
is same as
c = a | b;
if ( c == true)
{
do something;
}
The first is a logical-or, the latter a bitwise-or. HOWEVER, if the two operators (a and b in your example), are boolean, the bitwise-or is seen as logical-or without short circuiting. This can be be convenient at times.
For example, consider:
boolean getTrue() {
System.out.println("getTrue() called");
return true;
}
public static void main(String[] args) {
boolean a = getTrue() || getTrue();
System.out.println("Result: " + a);
}
The above will only print "getTrue() called" once as the logical-or (||) can determine the result of the expression immediately, without calling getTrue() a second time. Changing to a bitwise-or (i.e. boolean a = getTrue() | getTrue();) will result in two calls to getTrue().
A similar result will be produced with a bitwise-& operation and a getFalse() method.
Another aspect to keep into consideration is that the bit-wise operators gets preference before logical operators. Therefore, mixing them is not recommended as bitwise-or will be executed before a logical-and, which can cause unwanted behaviour. It can be fixed using brackets (), but I think this should be avoided.
I always thought that && operator in Java is used for verifying whether both its boolean operands are true, and the & operator is used to do Bit-wise operations on two integer types.
Recently I came to know that & operator can also be used verify whether both its boolean operands are true, the only difference being that it checks the RHS operand even if the LHS operand is false.
Is the & operator in Java internally overloaded? Or is there some other concept behind this?
& <-- verifies both operands
&& <-- stops evaluating if the first operand evaluates to false since the result will be false
(x != 0) & (1/x > 1) <-- this means evaluate (x != 0) then evaluate (1/x > 1) then do the &. the problem is that for x=0 this will throw an exception.
(x != 0) && (1/x > 1) <-- this means evaluate (x != 0) and only if this is true then evaluate (1/x > 1) so if you have x=0 then this is perfectly safe and won't throw any exception if (x != 0) evaluates to false the whole thing directly evaluates to false without evaluating the (1/x > 1).
EDIT:
exprA | exprB <-- this means evaluate exprA then evaluate exprB then do the |.
exprA || exprB <-- this means evaluate exprA and only if this is false then evaluate exprB and do the ||.
Besides not being a lazy evaluator by evaluating both operands, I think the main characteristics of bitwise operators compare each bytes of operands like in the following example:
int a = 4;
int b = 7;
System.out.println(a & b); // prints 4
//meaning in an 32 bit system
// 00000000 00000000 00000000 00000100
// 00000000 00000000 00000000 00000111
// ===================================
// 00000000 00000000 00000000 00000100
boolean a, b;
Operation Meaning Note
--------- ------- ----
a && b logical AND short-circuiting
a || b logical OR short-circuiting
a & b boolean logical AND not short-circuiting
a | b boolean logical OR not short-circuiting
a ^ b boolean logical exclusive OR
!a logical NOT
short-circuiting (x != 0) && (1/x > 1) SAFE
not short-circuiting (x != 0) & (1/x > 1) NOT SAFE
It depends on the type of the arguments...
For integer arguments, the single ampersand ("&")is the "bit-wise AND" operator. The double ampersand ("&&") is not defined for anything but two boolean arguments.
For boolean arguments, the single ampersand constitutes the (unconditional) "logical AND" operator while the double ampersand ("&&") is the "conditional logical AND" operator. That is to say that the single ampersand always evaluates both arguments whereas the double ampersand will only evaluate the second argument if the first argument is true.
For all other argument types and combinations, a compile-time error should occur.
&& is a short circuit operator whereas & is a AND operator.
Try this.
String s = null;
boolean b = false & s.isEmpty(); // NullPointerException
boolean sb = false && s.isEmpty(); // sb is false
I think my answer can be more understandable:
There are two differences between & and &&.
If they use as logical AND
& and && can be logical AND, when the & or && left and right expression result all is true, the whole operation result can be true.
when & and && as logical AND, there is a difference:
when use && as logical AND, if the left expression result is false, the right expression will not execute.
Take the example :
String str = null;
if(str!=null && !str.equals("")){ // the right expression will not execute
}
If using &:
String str = null;
if(str!=null & !str.equals("")){ // the right expression will execute, and throw the NullPointerException
}
An other more example:
int x = 0;
int y = 2;
if(x==0 & ++y>2){
System.out.print(“y=”+y); // print is: y=3
}
int x = 0;
int y = 2;
if(x==0 && ++y>2){
System.out.print(“y=”+y); // print is: y=2
}
& can be used as bit operator
& can be used as Bitwise AND operator, && can not.
The bitwise AND " &" operator produces 1 if and only if both of the bits in
its operands are 1. However, if both of the bits are 0 or both of the bits are different then this operator produces 0. To be more precise bitwise AND " &" operator returns 1 if any of the two bits is 1 and it returns 0 if any of the bits is 0.
From the wiki page:
http://www.roseindia.net/java/master-java/java-bitwise-and.shtml
it's as specified in the JLS (15.22.2):
When both operands of a &, ^, or | operator are of type boolean or Boolean, then the type of the bitwise operator expression is boolean. In all cases, the operands are subject to unboxing conversion (§5.1.8) as necessary.
For &, the result value is true if both operand values are true; otherwise, the result is false.
For ^, the result value is true if the operand values are different; otherwise, the result is false.
For |, the result value is false if both operand values are false; otherwise, the result is true.
The "trick" is that & is an Integer Bitwise Operator as well as an Boolean Logical Operator. So why not, seeing this as an example for operator overloading is reasonable.
‘&&’ : - is a Logical AND operator produce a boolean value of true or false based on the logical relationship of its arguments.
For example: - Condition1 && Condition2
If Condition1 is false, then (Condition1 && Condition2) will always be false, that is the reason why this logical operator is also known as Short Circuit Operator because it does not evaluate another condition. If Condition1 is false , then there is no need to evaluate Condtiton2.
If Condition1 is true, then Condition2 is evaluated, if it is true then overall result will be true else it will be false.
‘&’ : - is a Bitwise AND Operator. It produces a one (1) in the output if both the input bits are one. Otherwise it produces zero (0).
For example:-
int a=12; // binary representation of 12 is 1100
int b=6; // binary representation of 6 is 0110
int c=(a & b); // binary representation of (12 & 6) is 0100
The value of c is 4.
for reference , refer this http://techno-terminal.blogspot.in/2015/11/difference-between-operator-and-operator.html
&& and || are called short circuit operators. When they are used, for || - if the first operand evaluates to true, then the rest of the operands are not evaluated. For && - if the first operand evaluates to false, the rest of them don't get evaluated at all.
so if (a || (++x > 0)) in this example the variable x won't get incremented if a was true.
With booleans, there is no output difference between the two. You can swap && and & or || and | and it will never change the result of your expression.
The difference lies behind the scene where the information is being processed. When you right an expression "(a != 0) & ( b != 0)" for a= 0 and b = 1, The following happens:
left side: a != 0 --> false
right side: b 1= 0 --> true
left side and right side are both true? --> false
expression returns false
When you write an expression (a != 0) && ( b != 0) when a= 0 and b = 1, the following happens:
a != 0 -->false
expression returns false
Less steps, less processing, better coding, especially when doing many boolean expression or complicated arguments.
Besides && and || being short circuiting, also consider operator precedence when mixing the two forms.
I think it will not be immediately apparent to everybody that result1 and result2 contain different values.
boolean a = true;
boolean b = false;
boolean c = false;
boolean result1 = a || b && c; //is true; evaluated as a || (b && c)
boolean result2 = a | b && c; //is false; evaluated as (a | b) && c
& is a bitwise operator plus used for checking both conditions because sometimes we need to evaluate both condition.
But && logical operator go to 2nd condition when first condition give true.
all answers are great, and it seems that no more answer is needed
but I just wonted to point out something about && operator called dependent condition
In expressions using operator &&, a condition—we’ll call this the dependent condition—may require another condition to be true for the evaluation of the dependent condition to be meaningful.
In this case, the dependent condition should be placed after the && operator to prevent errors.
Consider the expression (i != 0) && (10 / i == 2). The dependent condition (10 / i == 2) must appear after the && operator to prevent the possibility of division by zero.
another example (myObject != null) && (myObject.getValue() == somevaluse)
and another thing: && and || are called short-circuit evaluation because the second argument is executed or evaluated only if the first argument does not suffice to determine the value of the expression
References: Java™ How To Program (Early Objects), Tenth Edition
In respect of the AND and OR operators, Java has got two types of evaluation namely Short-Circuit evaluation and full evaluation.
&& || Short-Circuit Evaluation
Short-Circuit evaluation enables you to not evaluate the right-hand side of AND and OR expressions, when the overall result can be predicted from the left-side value.
int numberOne = 1;
int numberTwo = 2;
boolean result = false;
// left-side is false so the the overall result CAN be predicted without evaluating the right side.
// numberOne will be 1, numberTwo will be 2, result will be false
result = (numberOne > numberTwo) && (++numberOne == numberTwo);
System.out.println(numberOne); // prints 1
System.out.println(numberTwo); // prints 2
System.out.println(result); // prints false
// left-side is true so the the overall result CAN NOT be predicted without evaluating the right side.
// numberOne will be 2, numberTwo will be 2, result will be true
result = (numberTwo > numberOne) && (++numberOne == numberTwo);
System.out.println(numberOne); // prints 2
System.out.println(numberTwo); // prints 2
System.out.println(result); // prints true
& | ^ Full Evaluation
Although in some cases it is possible to predict the result, It is necessary to evaluate the right-hand side.
int numberOne = 1;
int numberTwo = 2;
boolean result = false;
// left-side is false so the the overall result will be false BUT the right side MUST be evaluated too.
// numberOne will be 2, numberTwo will be 2, result will be false
result = (numberOne > numberTwo) & (++numberOne == numberTwo);
System.out.println(numberOne); // prints 2
System.out.println(numberTwo); // prints 2
System.out.println(result); // prints false
Notice:
Notice that for XOR (^) there is no short-circuit, because both sides are always required to determine the overall result.
Notice that other possible names for Short-Circuit evaluation are minimal evaluation and McCarthy evaluation.
It is not recommenced to mix boolean logic and actions in the same expression
& can also act as a Bitwise AND operator which is very academic and can be used in cryptography. When both bits are 1, the result is 1, or either of the bits is not 1, the result is 0. (Check the following code)
AND Bitwise example:
byte a = 5; // 00000101
byte b = 3; // 00000011
byte c = (byte) (a & b); // 00000001 (c is 1)
Almost every point of comparison is very well covered in all the answers. I just want to add one example. To demonstrate how the output changes based on which operator we use. Consider the below example
int a = 10;
if(++a==10 & ++a==12) {
++a;
}
System.out.println(a); //12
In the above code, We are using bitwise & operator. So It will evaluate both the arguments(left and right) irrespective of the individual result.
so a will increment 2 times within if condition. But as the condition will not become true, It will not enter inside the if-loop and 3rd increment will not happen. So the final value of a would become 12 in this case.
Now suppose, in the same above example If we use short-circuit && operator. then after evaluating ++a==10 to false, It will not go to check the second argument. And Hence the final value of a would-be 11.
int a = 10;
if(++a==10 && ++a==12) {
++a;
}
System.out.println(a); //11
Based on this, We can say that performance of bitwise & operator is relatively low compare to the short-circuit && operator. As bitwise operator will go to evaluate both the arguments irrespective of the result of the first argument. While && operator will stop evaluating the second argument if the first argument's result is false.
One more difference between these two is, Bitwise & operator is applicable for boolean as well as integral types. While short-circuit && operator is applicable only for the boolean type.
We can write
System.out.println(4 & 5); // 4
But if We try to write like ,
System.out.println(4 && 5);
Then it will give an error saying,
bad operand types for binary operator '&&'