logical-operators

Setting default values (conditional assignment)

有些话、适合烂在心里 提交于 2019-11-27 21:05:25
In Ruby you can easily set a default value for a variable x ||= "default" The above statement will set the value of x to "default" if x is nil or false Is there a similar shortcut in PHP or do I have to use the longer form: $x = (isset($x))? $x : "default"; Are there any easier ways to handle this in PHP? As of PHP 5.3 you can use the ternary operator while omitting the middle argument: $x = $x ?: 'default'; isset($x) or $x = 'default'; As of PHP 7.0, you can also use the null coalescence operator // PHP version < 7.0, using a standard ternary $x = (isset($_GET['y'])) ? $_GET['y'] : 'not set';

Use logical operator as combine closure in reduce

雨燕双飞 提交于 2019-11-27 17:32:19
问题 I am trying to reduce an array of Bool s by applying the logical operator OR ( || ) using the following code, however I get an error: func reduceBools(values: [Bool]) -> Bool { return values.reduce(false, combine: ||) } Ambiguous reference to member '||' Analogously for integers the code works like a charm. func reduceInts(values: [Int]) -> Int { return values.reduce(0, combine: +) } I was able to make it work by adding a || function (code below) or using a { $0 || $1 } closure but I dislike

Why use !!(condition) instead of (condition)? [duplicate]

我的未来我决定 提交于 2019-11-27 17:07:18
问题 This question already has an answer here: What does !!(x) mean in C (esp. the Linux kernel)? 3 answers I've seen code where people have used conditional clauses with two '!'s #define check_bit(var, pos) (!!((var) & (1 << (pos)))) #define likely(x) __builtin_expect(!!(x),1) #define unlikely(x) __builtin_expect(!!(x),0) are some of the examples I could find. Is there any advantage in using !!(condition) over (condition) ? 回答1: Well if the variable you are applying !! is not already a bool (

Is the “true” result of >, <, !, &&, || or == defined?

牧云@^-^@ 提交于 2019-11-27 15:27:52
When I for instance write 7>1 in C (say C99 if this is not an always-been feature), can I expect the result will be exactly 1 or just some non-zero value? Does this hold for all bool operators? In C99 §6.5.8 Relational Operators, item 6 ( < , > , <= and >= ): Each of the operators < (less than), > (greater than), <= (less than or equal to), and >= (greater than or equal to) shall yield 1 if the specified relation is true and 0 if it is false) The result has type int . As for equality operators, it's a bit further in §6.5.9 ( == and != ): The == (equal to) and != (not equal to) operators are

Is BitArray faster in C# for getting a bit value than a simple conjuction with bitwise shift?

依然范特西╮ 提交于 2019-11-27 15:27:42
1). var bitValue = (byteValue & (1 << bitNumber)) != 0; 2). using System.Collections.BitArray with a Get(int index) method What is faster? In what situations for the .NET projects BitArray could be more useful than a simple conjunction with the bitwise shift? BitArray is going to be able to handle an arbitrary number of boolean values, whereas a byte will hold only 8, int only 32, etc. This is going to be the biggest difference between the two. Also, BitArray implements IEnumerable , where a integral type obviously does not. So it all depends on the requirements of your project; if you need an

The difference between 'AND' and '&&' in SQL

社会主义新天地 提交于 2019-11-27 15:02:27
Is there a difference in the way SQL interprets the logical operators AND and && ? For mySQL: The manual is not saying it explicitly, but they are listed as identical: AND, && Logical AND. Evaluates to 1 if all operands are nonzero and not NULL, to 0 if one or more operands are 0, otherwise NULL is returned. The operator precedence page also makes no distiction. AND is Standard SQL && is proprietary syntax According to this page http://msdn.microsoft.com/en-us/library/bb387129.aspx they have the same functionality in SQL Server. MySql has this to say on the subject http://dev.mysql.com/doc

Is there any difference between && and & with bool(s)?

好久不见. 提交于 2019-11-27 13:30:34
问题 In C++, is there any difference between doing && (logical) and & (bitwise) between bool(s)? bool val1 = foo(); bool val2 = bar(); bool case1 = val1 & val2; bool case2 = val1 && val2; Are case1 and case2 identical or if not how exactly do they vary and why would one choose one over the other? Is a bitwise and of bools portable? 回答1: The standard guarantees that false converts to zero and true converts to one as integers: 4.7 Integral conversions ... If the destination type is bool, see 4.12.

Execution order of conditions in C# If statement

萝らか妹 提交于 2019-11-27 13:05:47
问题 There are two if statements below that has multiple conditions using logical operators. Logically both are same but the order of check differs. The first one works and the second one fails. I referred MSDN for checking whether the order of execution of the conditions defined; but I could not find. Consider a multiple check condition that has && as the logical operator. Is it guaranteed that it will always check the first condition and if that is not satisfied the second condition will not be

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

不羁的心 提交于 2019-11-27 11:19:00
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? John Feminella || is the logical-or operator. See here . It evaluates to true if at least one of the operands is true. You can only use it

Is it good practice to use the xor operator for boolean checks? [closed]

核能气质少年 提交于 2019-11-27 09:18:20
问题 I personally like the exclusive or , ^ , operator when it makes sense in the context of boolean checks because of its conciseness. I much prefer to write if (boolean1 ^ boolean2) { //do it } than if((boolean1 && !boolean2) || (boolean2 && !boolean1)) { //do it } but I often get confused looks from other experienced Java developers (not just the newbies), and sometimes comments about how it should only be used for bitwise operations. I'm curious as to the best practices regarding the usage of