logical-operators

Confused by use of double logical not (!!) operator [duplicate]

吃可爱长大的小学妹 提交于 2019-11-29 10:43:30
问题 This question already has answers here : Double Negation in C++ code (14 answers) What is “!!” in C? [duplicate] (7 answers) Closed 6 years ago . I have some C++ code that makes extensive use of !! . I'm kinda baffled because as far as I know !! is not a operator on it's own but two ! after each other. So that would mean that !!foo is the same as just foo . Is there any place and or reason when !! actually makes sense? I was thinking about if it could perhaps have a bit wise meaning? So you

Logical AND in Forth?

蓝咒 提交于 2019-11-29 09:23:27
I know the AND word defines binary and ... but what defines logical and ? The same word, AND , is also used for logical and . But the two input values to AND are recommended to be well-formed flags ; true and false are represented by two values, bits all set (-1) and bits all unset (0). Other values than these may work as true (as in C), but may lead to subtle errors. All comparison operators return well-formed flags, but for instance - does not. The following evaluates to false (0). 7 5 - 7 3 - AND AND gets bit patterns 100 and 010. The result is 0 (as it does the bitwise and ). References:

unusual ternary operation

烂漫一生 提交于 2019-11-29 09:20:39
I was asked to perform this operation of ternary operator use: $test='one'; echo $test == 'one' ? 'one' : $test == 'two' ? 'two' : 'three'; Which prints two (checked using php). I am still not sure about the logic for this. Please, can anybody tell me the logic for this. Nicholas Wilson Well, the ? and : have equal precedence, so PHP will parse left to right evaluating each bit in turn: echo ($test == 'one' ? 'one' : $test == 'two') ? 'two' : 'three'; First $test == 'one' returns true, so the first parens have value 'one'. Now the second ternary is evaluated like this: 'one' /*returned by

Boolean OR in sed regex

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-29 09:16:05
I'm trying to replace all references of a package named boots in a configuration file. The line format is add fast (package OR pkg) boots-(any-other-text) , e.g.: add fast package boots-2.3 add fast pkg boots-4.5 I want to replace it with: add fast pkg boots-5.0 I've tried the following sed commands: sed -e 's/add fast (pkg\|package) boots-.*/add yinst pkg boots-5.0/g' sed -e 's/add fast [pkg\|package] boots-.*/add yinst pkg boots-5.0/g' What's the right regex? I think I'm missing something in the boolean or ( package or pkg ) part. sed -e 's/add fast \(pkg\|package\) boots-.*/add yinst pkg

Get the maximum permutation matrix from logical matrix

夙愿已清 提交于 2019-11-29 08:42:23
A (m rows, n columns) is a (0,1)-Matrix (or logical matrix). How to get a sub matrix B (p rows, p columns) from A , satisfying that B is a permutation matrix and p is the maximum? For instance, PS: A permutation matrix is a square binary matrix that has exactly one entry 1 in each row and each column and 0s elsewhere. One possibility is to exploit that every permutation matrix can be built up one row and column at a time. So we can take every permutation matrix of a certain size, try to extend it by all possible rows or columns, and see what results in a permutation matrix that is one size

How do I use the bitwise operator XOR in Lua?

旧城冷巷雨未停 提交于 2019-11-29 07:53:20
问题 How can I implement bitwise operators in Lua language? Specifically, I need a XOR operator/method. 回答1: In Lua 5.2, you can use functions in bit32 library. In Lua 5.3, bit32 library is obsoleted because there are now native bitwise operators. print(3 & 5) -- bitwise and print(3 | 5) -- bitwise or print(3 ~ 5) -- bitwise xor print(7 >> 1) -- bitwise right shift print(7 << 1) -- bitwise left shift print(~7) -- bitwise not Output: 1 7 6 3 14 -8 回答2: In Lua 5.2, you can use the bit32.bxor

Difference between “!= true” and “== false”?

我们两清 提交于 2019-11-29 07:49:56
Are there any technical/logical differences between the comparison "!= true" and "== false" in programming languages, and if there are, which comparison should be chosen on what occasion? Logically there can be differences depending on the type of value that you are comparing and language you are using. For example: x == false implies x != true , but x != true does not always imply x == false because x can also be some nonsense value. 1 + 1 = 3 is both == false and != true . 7 > cat is neither == false and != true since it is nonsense. x = null is != true but is not == false . 来源: https:/

Is 'IS DISTINCT FROM' a real MySQL operator?

有些话、适合烂在心里 提交于 2019-11-29 07:49:37
In one book I see this syntax: SELECT * FROM inw WHERE id IS DISTINCT FROM 4; But I get an error: ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DISTINCT FROM 4' at line 1 It's an alternative for: mysql> SELECT * FROM inw WHERE id is null OR id <> 4; +------+ | id | +------+ | NULL | | NULL | | 3 | +------+ Is 'IS DISTINCT FROM' a real MySQL operator? is distinct from is defined in the SQL:2003 standard and is a null-safe operator to compare two values. MySQL supports a "null safe equals

What is the point of the logical operators in C?

对着背影说爱祢 提交于 2019-11-29 07:35:31
I was just wondering if there is an XOR logical operator in C (something like && for AND but for XOR). I know I can split an XOR into ANDs, NOTs and ORs but a simple XOR would be much better. Then it occurred to me that if I use the normal XOR bitwise operator between two conditions, it might just work. And for my tests it did. Consider: int i = 3; int j = 7; int k = 8; Just for the sake of this rather stupid example, if I need k to be either greater than i or greater than j but not both, XOR would be quite handy. if ((k > i) XOR (k > j)) printf("Valid"); else printf("Invalid"); or printf("%s"

Behaviour of && in C programming language

会有一股神秘感。 提交于 2019-11-29 05:29:53
I am beginner in C programming language, recently I have read about Logical AND && operator. I also know that, in C programming language all non-zero values are treated as TRUE . NON-ZERO && NON-ZERO = 1 NON-ZERO && ZERO = 0 ZERO && NON-ZERO = 0 ZERO && ZERO = 0 But when I am dealing with the following program then I am not getting expected answer. int main(){ int x, y, z; x = y = z = -1; y = ++x && ++y && ++z; printf("x = %d, y = %d, z = %d, x, y, z); return 0; } I am expecting x = 0, y = 0, z = 0 but the answer is x = 0, y = 0, z = -1 Can anyone please explain, Why I am getting this answer?