This question already has answers here:
Are there good uses for non-short-circuiting logical (boolean) operators in Java/Scala?
(4 answers)
What is the difference between the | and || or operators?
(12 answers)
Why doesn't the bitwise & operator short-circuit?
(5 answers)
Closed 5 years ago.
I am trying to figure out the difference between & and && in Scala. I got this after searching
& <-- verifies both operands
&& <-- stops evaluating if the first operand evaluates to false since the result will be false
Can somebody explain the same with example as am not clear, what verifying both operands means here. Are we talking about just Booleans?
Both are logical AND operators in Scala. The first form, &, will evaluate both operands values, for example:
val first = false
val second = true
if (first & second) println("Hello!")
Will evaluate both first and second before exiting the if condition, although we know that once a false appears in a logical AND, that entire expression will already yield false.
This is what && is for, and what it does is short-circuit the evaluation, meaning you only ever evaluate firsts value and then exit the conditional.
You can use bitwise AND (&) to perform bitwise operations on integers, which is similar to most programming languages.
Related
This question already has answers here:
What do curly braces mean in Verilog?
(2 answers)
Closed 3 years ago.
I am starting to learn SystemVerilog. I am stuck with a priority encoder and can't get this part :
priority if (encoder_in == {{14{1'bx}},1'b1,{1{1'b0}}})
this is actually a concatenation of 3 constants.
{ {14{1'bx}}, 1'b1, {1{1'b0}} }
1--^^^^^^^^^^
2--------------^^^^
3--------------------^^^^^^^^^
is a replication operator and it generates a 14-bits of 'x'.
is a one-bit 1
is a replication operator with a single repetition. I have no idea why it is used this way.
The following would be an equivalent expression:
{{14{1'bx}}, 1'b1, 1'b0}
or this
{{14{1'bx}}, 2'b10}
or this:
16'xxxxxxxxxxxxxx10
Next, priority is a system verilog modifier which could be applied to an if or a case operators. Read about unique end priority modifiers in system verilog.
This question already has answers here:
toList on Range with suffix notation causes type mismatch
(2 answers)
Closed 5 years ago.
I am learning postfix unary operators in Scala.
The following can not compile:
val result = 43 toString
println(result)
However if I add one empty line inbetween the two lines, the code compile and produces right output:
val result = 43 toString
println(result)
What is the difference between these two segments?
BTW, I did not add "import scala.language.postfixOps".
Perhaps the issue is clearer if we use some other operator instead of toString.
// This parses as `List(1,2,3,4) ++ List(4,5,6)`
List(1,2,3,4) ++
List(4,5,6)
Basically, in order to make the above work, while also allowing things like foo ? (a postfix operator), Scala needs to know when it is OK to stop expecting a second argument (and accept that the expression is a postfix operator).
Its solution is give up on finding a second argument if there is an interceding new line.
This question already has answers here:
Is Perl optimized to skip remaining logic operands if the answer is already decided?
(6 answers)
Closed 7 years ago.
if(expression1 or expression 2)
{
do something
}
If 'expression1' returns true, does the compiler starts to execute 'do something' or it evaluates the second expression too?
No, expression2 will not be evaluated if expression1 is true.
This is because or is short-circutted in perl: once the result of the entire expression is known, evaluation stops. Evaluation occurs from left to right.
This question already has answers here:
What do all of Scala's symbolic operators mean?
(11 answers)
Closed 8 years ago.
What does :: mean here ?
listeners ::= listener
list = num :: list
Specially I don't understand the "::" operator.
Its the list cons operator. It creates a new list whose head is first argument and whose tail is contents of the second argument.
This question already has answers here:
Meaning of // operator in perl
(5 answers)
Closed 9 years ago.
I was searching in a lot of Perl books but I can't find an answer. I have this code, what I suppose it does is assign param's ticket to $ticket iff it exists if not, assign 0.
my $ticket = $params->{ticket} // 0;
// means defined-or. $ticket is assigned $params->{ticket} if it is defined, 0 otherwise.
Although it has no direct equivalent in C, Perl's // operator is related to its C-style or. In fact, it's exactly the same as ||, except that it tests the left hand side's definedness instead of its truth. Thus, EXPR1 // EXPR2 returns the value of EXPR1 if it's defined, otherwise, the value of EXPR2 is returned.
It was added in 5.10.
In the code above, $params->{ticket} can still have garbage in it, so make sure the value conforms to the expected pattern before using it.
Perl documentation says:
"EXPR1 // EXPR2 returns the value of EXPR1 if it's defined, otherwise, the value of EXPR2 is returned."
It's similar to a logic or, but testing definedness.