My question is operator related. Today I studied about the operators. Having a confusion. In PHP what is the difference between "or" and "xor". I know both of them are related to Boolean expression. But can't find the original difference.
Anyone please help to understand it more clearly.
It has to do with mutual exclusion. xor is exclusive. or is inclusive.
Truth Table Comparison
$x $y ($x or $y) ($x xor $y)
0 0 0 0
1 0 1 1
0 1 1 1
1 1 1 0
Note: the difference in the last case. xor is only true when either $x or $y is true, but not both (as the case for or).
xor means "exclusive or". That is to say, it's or, but with the single change that if both parameters to the operation are true, the answer is false.
A xor B == (A or B) && !(A and B)
The difference is with an input of an even amount of 1's (or true's):
true or true = true
true xor true = false
true or true or true = true
true xor true xor true = true
true or false or true = true
true xor false xor true = false
Truth table of OR:
in1 | in2 | out
-------|-------|-----
0 | 0 | 0
0 | 1 | 1
1 | 0 | 1
1 | 1 | 1
Truth table of XOR:
in1 | in2 | out
-------|-------|-----
0 | 0 | 0
0 | 1 | 1
1 | 0 | 1
1 | 1 | 0
Another way of putting it: OR can be seen as an AND/OR connector. One or both choices can be true or chosen. XOR, on the other side, could be described as a "real" OR. Only one of both choices can be true (or chosen).
one is exclusive or -> xor, and the other is or which means at least one of the conditions are met in a truth table. Or is not mutually exclusive, xor is.
enter image description here
xor is exclusive. or is inclusive
Related
demorgans law states in this article
https://ryanstutorials.net/boolean-algebra-tutorial/boolean-algebra-laws.php#introduction
that NOT(P & K) = NOT(P) OR NOT(K)
so i did a little testing with 1's and zeros
NOT(1 AND 1) = false
NOT(1 AND 0) = false
NOT(0 AND 1) = false
NOT(O AND 0) = true
so then i did the second expression
NOT(1) OR NOT(O) = TRUE
NOT(0) OR NOT(1) = TRUE
NOT(0) OR NOT(0) = TRUE
NOT(1) OR NOT(1) = FALSE
what am i doing wrong here?
You have to do this table :
P K | Q Q'
0 0 | 0 1
0 1 | 0 1
1 0 | 0 1
1 1 | 1 0
The Q is the Output and the Q' is the Output', 0=false and 1=true;
So, the first column is obtained doing P*K and the second denying Qso P'+K'.
if ((status & 0x3F) == 1 ){ }..
the status is variable in swift language.
what is mean about this condition, & mean and (status & 0x3F) value return
& is the bitwise AND operator. It compares the bits of the two operands and sets the corresponding bit to 1 if it is 1 in both operands, or to 0 if either or both are 0.
So this statement:
((status & 0x3F) == 1)
is combining status with 0b111111 (the binary equivalent of 0x3F and checking if the result is exactly 1. This will only be true if the last 6 bits of status are 0b000001.
In this if:
if( (dtc24_state[2] & 0x8) == 0x8 ) {
self.haldexABCDTC24State.text = status_str + " - UNKNOWN"
self.haldexABCDTC24State.textColor = text_color
active_or_stored_dtc = true
}
dct24_state is an array of values. The value of dct24_state[2] is combined with 0x8 or 0b1000 and checked against 0x8. This is checking if the 4th bit from the right is set. Nothing else matters. If the 4th bit from the right is set, the if is true and the code block is executed.
0x3F is 111111. So, it means this:
for each bit of yourNumber in binary system presentation use and method.
This way truncates the left part of the number. and the result compares with 1.
e.g.
7777 is 1111001100001 after executing and this number converts into
100001. So the result is false.
But for 7745 (1111001000001) the result is 1. The result is true.
The rule for 'and' function: 0 & 0 = 0 ; 0 & 1 = 0; 1 & 0 = 1; 1 & 1 = 1.
I am trying to understand Boolean logic and operators.
I found this example but can't understand why this expression will evaluate to the one shown below.
Say, a = 0, b = 1, c = 0
Expression Will Evaluate to
val1 = !(a || b || c); !(0 || 1 || 0) = !(1) = 0
As I see it, val1 is not a or not b or not c, so why it evaluates to not 1 ?
Not(a or b or c) evaluates the or operations first, so it's not the same as (not a) or (not b) or (not c).
Indeed, it's the same as (not a) AND (not b) AND (not c).
Either operand to an OR being true will give a true result, and then the NOT flips that to a false result for the expression as a whole.
As with integer or real number arithmetic, order of operation can greatly alter the result.
.... val1 is not a or not b or not c ...
No, this is incorrect. The 0 || 1 || 0 inside the parenthesis is evaluated first. The example has it right.
Let's say val1 = 1
1 = !(0 || 1 || 0)
1 = !(1) - because it is the only value that is equal to val1
1 = 0 - then it negates it afterwards
Let's go step-by-step.
val1 = !(0 || 1 || 0);
Firstly, 0 || 1 will evaluate to 1, because || means 'true if at least one of them is true, otherwise false', and 1 = true, 0 = false.
So now it is
val1 = !(1 || 0); Here 1 || 0 will again evaluate to 1, because at least one of them is 1. Now we've got val1 = !(1);. ! means the opposite of the input, so !(1) = 0.
As I see it, val1 is not a or not b or not c, so why it evaluates to not 1 ?
Because what you say would be written as val1 = !0 || !1 || !0. Its quite different, because it doesn't have parenthesis. Parenthesis means 'evaluate everything in the parenthesis first'.
I have tried to create a array of custom objects (pscustomobject) and now i tried to change some values of the different custom objects with an for-loop. But it doesn't seem to work. Here is what i tried:
$obj = #([pscustomobject]#{value=0;type="D";used=$false})
$arr1 = #($obj) * 10
for($v = 0; $v -lt 4; $v++){
$arr1[$v].value = ($v+1)
$arr1[$v].type ="bubble"
}
The result is:
value type used
----- ---- ----
4 bubble False
4 bubble False
4 bubble False
4 bubble False
4 bubble False
4 bubble False
4 bubble False
4 bubble False
4 bubble False
4 bubble False
But i expected that the result will be:
value type used
----- ---- ----
1 bubble False
2 bubble False
3 bubble False
4 bubble False
4 D False
4 D False
4 D False
4 D False
4 D False
4 D False
This is only a snipped, and i was only trying something out. But i am a bit annoyed that i don't get it... Sorry i think it's a easy think but don't see whats worong.... i am still a PS noob... :-/
EDIT: PSv3 is used, but i think it doesn't matter...
This:
$arr1 = #($obj) * 10
is not creating 10 new objects. It created 10 references to the same object.
Note the difference if you do this:
$arr1 = 1..10 |% {[pscustomobject]#{value=0;type="D";used=$false}}
for($v = 0; $v -lt 4; $v++){
$arr1[$v].value = ($v+1)
$arr1[$v].type ="bubble"
}
$arr1
value type used
----- ---- ----
1 bubble False
2 bubble False
3 bubble False
4 bubble False
0 D False
0 D False
0 D False
0 D False
0 D False
0 D False
If a statement like ~(5>4) || (1>10) is given what does it evaluate to in MATLAB?
What I think is the answer is :
~1 || 0 = 0 || 0
= 0
so it evaluates to false.
Am I correct in that the not operator only attaches to the (5 > 4)?
In this case, Matlab would evaluate the logical operators from left to right. See Operator Precedence for instances when this would not be the case.
That would mean that ~(5>4) | (1>10) would first evaluate the left half of the OR:
~(5>4) --> ~(1) --> 0
Then continuing...
0 | (1>10) --> 0 | 0 --> 0