logical OR operator vs bitwise OR operator - java

does anyone know why:
if (false && true || true) {
System.out.println("True");
} else {
System.out.println("False");
}
Print "True"
if (false && true | true) {
System.out.println("True");
} else {
System.out.println("False");
}
Print "False"

In the first case && has higher precedence than || operator so the expression is evaluated as if ( (false && true) || true ) and you get True.
In the second case bitwise OR operator has higher precedence than && so the expression is evaluated as if ( false && ( true | true ) ) and you get False.

Because of operator precedence. In your first example, the && is done first, and then the ||. But the bitwise OR has higher precedence, so in your second example the | is done first, then the &&.

Related

How to check if all boolean are true or false in Java?

I'm trying to figure out how to check if three variables are all true or all false. So that the condition becomes true, when these variables have the same value and false, when they don't have the same value.
I thought something like (d == e == f) would help me here but the condition is only true when all variables are set to true. But when they're set to false, the condition doesn't work. Could anyone explain to me why? I know a very basic issue but I really can't figure it out myself.
You can try like this :
if((d && e && f) || (!d && !e && !f))
It will enter in loop either all will be true or all will be false.
It's because all expressions having relational operators return Boolean value.
So, first e == f is evaluated. As these both are false (both operators having same value) so, this expression return true value.
This true value is evaluated against d which is false. So that expression returns false (as both Operators have different values now) .
To know if the 3 variables are all true or all false; this what you can do:
boolean allTrue, allFalse;
if(a && b && c) allTrue = true; //a, b & c will evaluate to boolean and if all three vars are true the condition will be true and hence the if statement will be accessed
if(!a && !b && !c) allFalse = true; //if the not of the 3 is true, i.e (the 3 boolean vars are false), the whole condtion will be true and the if-statement will be accessed and the allFalse will be set to true means all 3 are false
boolean allTrue=false;
boolean allFalse=false;
boolean a,b,c;//your three variables
if(a && b && c)
{allTrue=true;}
else if(!a && !b && !c)
{allFalse=true;}
Try this, here i have two variable flags that are set to false initially, when one of the condition is true then only it woll be set to true so after the last line of code you can check if allFalse or allTrue has values true or false.
It should be possible to stay closer to the original formulation like this:
boolean eitherAllTrueOrAllFalse = (d == e) && (d == f)
If you only need to know if all are true or all false, then this will be more than enough:
boolean flagAllTrue = a && b && c;
No need to use a if else.
First think of the conditions separately:
boolean allTrue = a && b && c;
boolean allFalse = !(a||b||c);
Then combine them:
boolean eitherAllTrueOrAllFalse = allTrue|allFalse;
let sum = 0;
sum += a ? 1:0; sum += b ? 1:0; sum += c ? 1:0;
let allTrueOrAllFalse = ( sum === 0 || sum === 3 );

Simple logic not working or am I missing something?

I don't understand why the following class prints out:
true
false
I thought the output should be:
false
false
because this line prints false:
System.out.println((11 >= 1 || 11 <= 10) & (true == false));
so this line should also print false:
System.out.println(in1To10(11, false));
What am I missing here? Here's the class.
public class TestClass {
public static void main(String[] args) {
System.out.println(in1To10(11, false));
System.out.println((11 >= 1 || 11 <= 10) & (true == false));
}
public static boolean in1To10(int n, boolean outsideMode) {
if ((n >= 1 || n <= 10) & (outsideMode == false)) {
return true;
}
return false;
}
}
You want to test if value is in the range 1 to 10 ?
If thats the case, change (n >= 1 || n <= 10) to (n >= 1 && n <= 10)
( true )
( true ) & ( true )
(true || false ) & ( true ) <--- false == false is true!
if ((n >= 1 || n <= 10) & (outsideMode == false)) {
return true;
}
Look, is n >= 1? Yes, so it's true. true v p <-> true, so Java doesn't even check further. true /\ true <-> true so we enter if. return true;
Your code is in plain English:
if ((n is greater or equal to 1 OR smaller or equal 10) AND is the mode set to false)
return true
If you want it to return true if the number is between 1 and 10 incl, it would be:
if ((n is greater or equal to 1 AND smaller or equal 10) AND is the mode set to false)
return true
which in Java is:
if ((n >= 1 && n <= 10) && (outsideMode == false)) {
return true;
}
Also remember to use && and || as they are logical operators instead of | and & bitwise logical operators when dealing with boolean values.
so this line should also print false: System.out.println(in1To10(11, false));
No, this line should not print false. Although the first parameter, 11, indeed turns the expression n >= 1 || n <= 10 from your method into 11 >= 1 || 11 <= 10, which matches your other expression, the second parameter, false, turns outsideMode == false into false == false, while your other expression has true == false.
That is why the two outputs are different: the output from in1To10 is true because the comparison false == false produces true, while the output from main is false, because true == false produces false.
Note: your expression does not match its stated goal of checking if n is between 1 and 10, inclusive. You need to replace || with && to accomplish that.
I think you missed that in function in1to10, you are comparing (false == false) and in main you are comparing (true == false). And so is the result. Hope this helps.
Let's decompose the test in in1to10 :
Parameters : outsideMode = false; n = 11;
(n >= 1 || n <= 10) :=> (11 >= 1 || 11 <= 10) :=> (true || false) :=> so TRUE
outsideMode == false :=> false == false :=> so TRUE
In the end :
public static boolean in1To10(int n, boolean outsideMode) {
if ((TRUE) & (TRUE)) {
return true;
}
return false;
}
:=> return TRUE !
This will return true. How?
if ((n >= 1 || n <= 10) & (outsideMode == false)) {
return true;
}
You are passing 11 and false to the function.
When it goes to inside the first condition it will check that n >= 1 mean 11 >= 1 that is true, so it will not check n<=10. Again it will check the second condition and your outsideMode is false,
That will be like this, (true) & (true)
hence the whole condition will be true and the funciton will return true.
Again the second condition,
(11 >= 1 || 11 <= 10) & (true == false)
It will return false. How?
As 11 >= 1 is true and again it will not check 11 <= 10, and right side is false.
So the condition will be become like this,
true & false that will be false.

Logic evaluation with && and || in a statement

I'm just debugging some code and found some expression:
if (mObject != null && a || b )
{
mObject.someMethod();
}
which had a NullPointerException in the mObject.someMethod() line - so my question is:
how is the expression evaluated?
1. a && b || c == a && (b || c)
or
2. a && b || c == (a && b) || c
If 1. is the case i'd probably have to look at some multithreading issues.
This is probably a dumb question and i'm pretty sure it has been asked before, but i can't find the answer here. Thanks for the answers in advance.
It is equivalent to the following condition:
if ((mObject != null && a) || b )
&& has a higher precedence that ||.
If mObject is null, the condition may pass if b is true.
!= has precedence over && which has precedence over || (Source)
Therefore
if (mObject != null && a || b )
is equivalent to
if (((mObject != null) && a) || b )
To avoid the exception, use parentheses :
if (mObject != null && (a || b) )
According to here, it's:
((( mObject != null ) && a ) || b )
so you should be looking into b, because:
(null != null) is false
false && a is always false
everything depends on b then.
If parentheses are not provided then it will be always evaluated from left to right of the equals expression.

Confusing with "&&" and "||"

What are the difference between case 1 and other two cases?
Case 1 : false && false || true
Case 2 : (false && false) || true
Case 3 : false && ( false || true )
&& has higher precedence than ||, so case 1 is equivalent to case 2.
See: http://en.cppreference.com/w/cpp/language/operator_precedence
Actually, case 1 and case 2 are equivalent expressions, but there can be a situation where the expression tree itself is different, like here:
true == false && false || true // A
true == (false && false) || true // B
// not the same!
Here they read as:
((true == false) && false) || true // A
(true == (false && false)) || true // B
because == has an even higher priority than &&.
&& is noted by * in boolean algebra, and || is noted by +. read it here.
The C/C++ operator precedence is derived usually from the mathematical precedences.
Case 1 : false && false || true => true
Case 2 : (false && false) || true => true
Case 3 : false && ( false || true ) => false
operation in brackets should always be executed before other operations.
case 2 : (false && false) || true => false || true => true
case 3 : false && ( false || true ) => false && true => false
case 1 : false && false || true => false || true => true
For case 3, short circuit evaluation causes the expression in the parenthesis after && to be ignored.
Short-circuit_evaluation

How to read this NOT operator?

I don't understand what the bolded part actually represents. Please correct me if I'm wrong.
30 does NOT equal to 30 is false OR NOT(17 equals to 17 (true) AND 20 is greater then 21(false)) // i don't get this one, do I just flip everything meaning like -1 before() so the statement would be:
17!=17(false) && 20<21(true). I'm lost here. Thanks for any advice.
boolean m;
m = ((30!=30) || !(17==17 && 20>21))
SOP(m);
true || false ?
The ! operator works on a single boolean type and returns the opposite of it's value.
Let's break this all down:
(30!=30 || !(17==17 && 20>21))
(false || !(true && false))
(false || !( false ))
(false || true ))
( true )
Let's consider the comparison to the left of the OR... that is 30 != 30. This is false. So
m = (false || !(17==17 && 20>21))
Next 17 is equal to 17,
m = (false || !(true && 20>21))
Next 20 is not > then 21, so false....
m = (false || !(true && false))
Next true and false is false... so
m = (false || !(false))
And that allows us to reach
m = (false || true)
So
m = true
m will equal true because...
30!=30 = false
17==17 = true
20>21 = false
so...
( false || ! ( true && false ) )
so...
( false || ! false )
so...
( false || true )
so..
true
I always find it helps to expand your calculations so to speak.
This condition will be true
boolean m = (A || B)
Here A is (30!=30)
and B is `!(17==17 && 20>21)`
Obviously, A wiil be false;
!(17==17 && 20>21) equals to !(true && false), equals to !false, equals to true.
Then m = (A||B) equals to (false || true) equals to true
Lets go through this step by step.
Initial problem:
m = ((30!=30) || !(17==17 && 20>21))
We know 30 equals 30 so the 30 != 30 is false:
m = ((false) || !(17==17 && 20>21))
Because it is an or statement, we can remove the false part:
m = !(17==17 && 20>21)
We know 17 is equal to 17 (or the world would be ending) and 20 is less than 21 so:
m = !(true and false)
True and false evaluates to false so we have:
m = !false
This makes m become true.

Categories

Resources