Using the 'not' operator in Boolean statements for Matlab - matlab

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

Related

what is mean by swift condition is if ((status & 0x3F) == 1 ){ }

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.

How to normalize Integer variable to positive negative or zero with bit operations

7 -> 1
0 -> 0
-7 -> -1
I've have code:
(x == 0 ? 0 : x / abs(x)) + 1
but is it possible to avoid division and make it faster?
How about
(x == 0 ? 0 : (x < 0 ? -1 : 1))
The idea was to use bit operations to avoid branching code or value conversion.
Haven't found how to do it with bit operations but apple already add this function
https://developer.apple.com/documentation/swift/int/2886673-signum
signum()
Returns -1 if this value is negative and 1 if it’s positive; otherwise, 0.
so simple) raw test shows ~x100 faster implementation

Why !(0 || 1 || 0) is 0?

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'.

Difference between or and xor

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

What does !! (double exclamation point) mean?

In the code below, from a blog post by Alias, I noticed the use of the double exclamation mark !!. I was wondering what it meant and where I could go in the future to find explanations for Perl syntax like this. (Yes, I already searched for !! at perlsyn).
package Foo;
use vars qw{$DEBUG};
BEGIN {
$DEBUG = 0 unless defined $DEBUG;
}
use constant DEBUG => !! $DEBUG;
sub foo {
debug('In sub foo') if DEBUG;
...
}
UPDATE
Thanks for all of your answers.
Here is something else I just found that is related The List Squash Operator x!!
It is just two ! boolean not operators sitting next to each other.
The reason to use this idiom is to make sure that you receive a 1 or a 0. Actually it returns an empty string which numifys to 0. It's usually only used in numeric, or boolean context though.
You will often see this in Code Golf competitions, because it is shorter than using the ternary ? : operator with 1 and 0 ($test ? 1 : 0).
!! undef == 0
!! 0 == 0
!! 1 == 1
!! $obj   == 1
!! 100 == 1
undef ? 1 : 0 == 0
0 ? 1 : 0 == 0
1 ? 1 : 0 == 1
$obj ? 1 : 0 == 1
100 ? 1 : 0 == 1
not-not.
It converts the value to a boolean (or as close as Perl gets to such a thing).
Because three other answers claim that the range is "0" or "1", I just thought I'd mention that booleans in Perl (as returned by operators like ==, not, and so on) are undef and 1, not 0 and 1.