logical-operators

A clear, layman's explanation of the difference between | and || in c#?

自古美人都是妖i 提交于 2019-12-17 10:26:23
问题 Ok, so I've read about this a number of times, but I'm yet to hear a clear, easy to understand (and memorable) way to learn the difference between: if (x | y) and if (x || y) ..within the context of C#. Can anyone please help me learn this basic truth, and how C# specifically, treats them differently (because they seem to do the same thing). If the difference a given piece of code has between them is irrelevant, which should I default to as a best-practise? 回答1: || is the logical-or operator.

Logical operator || in javascript, 0 stands for Boolean false?

China☆狼群 提交于 2019-12-17 07:39:15
问题 I happened to know the following code Here is the code, and very simple: var test = 0 || -1 ; console.log(test); then the output in the console is -1 and somehow i am really new into the javascript, all i think of is that the 0 stands for Boolean False in JS ,and so || operator seems to ignore the 0 and assign the value -1 to the variable so am i right ? i just want a confirm 回答1: || — expr1 || expr2 (Logical OR) Returns expr1 if it can be converted to true; otherwise, returns expr2. Thus,

Is there any wisdom behind “and”, “or” operators in Ruby?

蹲街弑〆低调 提交于 2019-12-17 06:52:48
问题 I wonder why ruby give and , or less precedence than && , || , and assign operator? Is there any reason? 回答1: My guess is that's a direct carry-over from Perl. The operators or and and were added later in Perl 5 for specific situations were lower precedence was desired. For example, in Perl, here we wish that || had lower precedence, so that we could write: try to perform big long hairy complicated action || die ; and be sure that the || was not going to gobble up part of the action. Perl 5

Using multiple criteria in subset function and logical operators

孤街醉人 提交于 2019-12-17 06:09:49
问题 If I want to select a subset of data in R, I can use the subset function. I wanted to base an analysis on data that that was matching one of a few criteria, e.g. that a certain variable was either 1, 2 or 3. I tried myNewDataFrame <- subset(bigfive, subset = (bigfive$bf11==(1||2||3))) It did always just select values that matched the first of the criteria, here 1. My assumption was that it would start with 1 and if it does evaluate to "false" it would go on to 2 and than to 3, and if none

Is relying on && short-circuiting safe in .NET?

好久不见. 提交于 2019-12-17 04:03:27
问题 Assume myObj is null. Is it safe to write this? if(myObj != null && myObj.SomeString != null) I know some languages won't execute the second expression because the && evaluates to false before the second part is executed. 回答1: Yes. In C# && and || are short-circuiting and thus evaluates the right side only if the left side doesn't already determine the result. The operators & and | on the other hand don't short-circuit and always evaluate both sides. The spec says: The && and || operators are

Logical operators in JavaScript — how do you use them?

六月ゝ 毕业季﹏ 提交于 2019-12-17 02:24:46
问题 I don't understand how && , || , and ! work... both with bools and other data types. How do you use them? 回答1: All values in Javascript are either "truthy" or "falsy". a && b evaluates to the first falsy operand a || b evaluates to the first truthy operand Both operators will not evaluate any operands after the one the return. If all operands don't match, it will evaluate to the last one. !a evaluates to true if a is falsy and false if a is truthy. All values are truthy except the following,

How do you get the logical xor of two variables in Python?

感情迁移 提交于 2019-12-16 22:10:06
问题 How do you get the logical xor of two variables in Python? For example, I have two variables that I expect to be strings. I want to test that only one of them contains a True value (is not None or the empty string): str1 = raw_input("Enter string one:") str2 = raw_input("Enter string two:") if logical_xor(str1, str2): print "ok" else: print "bad" The ^ operator seems to be bitwise, and not defined on all objects: >>> 1 ^ 1 0 >>> 2 ^ 1 3 >>> "abc" ^ "" Traceback (most recent call last): File "

Differences in boolean operators: & vs && and | vs ||

て烟熏妆下的殇ゞ 提交于 2019-12-16 20:17:28
问题 I know the rules for && and || but what are & and | ? Please explain these to me with an example. 回答1: Those are the bitwise AND and bitwise OR operators. int a = 6; // 110 int b = 4; // 100 // Bitwise AND int c = a & b; // 110 // & 100 // ----- // 100 // Bitwise OR int d = a | b; // 110 // | 100 // ----- // 110 System.out.println(c); // 4 System.out.println(d); // 6 Thanks to Carlos for pointing out the appropriate section in the Java Language Spec (15.22.1, 15.22.2) regarding the different

Can anyone explain to me what is going on in this line of MatLAB code

本秂侑毒 提交于 2019-12-14 04:13:37
问题 y = rand(20,3); aa= unidrnd(2,20,3) - 1; val = ( aa & y<1.366e-04) | (~aa & y<8.298e-04); aa(val) = ~aa(val); I have this code. Can any one explain to me what is happening here. I have tried to understand it step by step (debugging) but I cannot understand the purpose of using inverse '~' in line 4 and also using 'val' as indices. 回答1: y = rand(20,3); Creates a matrix of uniformly distributed random numbers, y . aa= unidrnd(2,20,3) - 1; Creates a matrix of uniformly distributed random

Multiple logical operators in one line of code

倾然丶 夕夏残阳落幕 提交于 2019-12-13 10:52:41
问题 I was searching on Stack Overflow for the answer to this question but I haven't found an exact answer. I came up with this code. I know how operators are supposed to work but I don't understand them in this kind of problem. For example, in the first case, how can z and y still be 1 if there I am using ++y and ++z ? #include <stdio.h> int main(void) { int x, y, z; x = y = z = 1; ++x || ++y && ++z; printf("x = %d y = %d z = %d\n", x, y, z); x = y = z = 1; ++x && ++y || ++z; printf("x = %d y =