boolean-logic

Why is the boolean expression “1 in (1, 2, 3) == True” False? [duplicate]

心已入冬 提交于 2019-12-23 12:18:59
问题 This question already has answers here : Why does (1 in [1,0] == True) evaluate to False? [duplicate] (1 answer) Why does the expression 0 < 0 == 0 return False in Python? (9 answers) Closed 3 years ago . Why does the statement 1 in (1, 2, 3) == True return False in Python? Is the operator priority in Python ambiguous? 回答1: Because, per the documentation on operator precedence: Note that comparisons, membership tests, and identity tests, all have the same precedence and have a left-to-right

How should boolean expressions be written in PHP?

我是研究僧i 提交于 2019-12-23 08:58:51
问题 How should the following boolean expression be written in PHP: $foo = ""; if($var==TRUE){ $foo = "bar"; } or if($var==TRUE){ $foo = "bar"; }else{ $foo = ""; } or $foo = ($var==TRUE) ? "bar": ""; 回答1: First off, true is not a constant, it's a token, so please don't uppercase it (I know some standards do that, but I think it confuses the meaning)... Second, you don't need the redundant $var == true comparison inside the if . It's exactly the same as if ($var) { (For a double == comparison. An

Checking type of variable against multiple types doesn't produce expected result

孤人 提交于 2019-12-23 05:26:06
问题 Task: Define a function, distance_from_zero with one parameter. Have that function do the following: Check the type of the input it receives. If the type is int or float , the function should return the absolute value of the function input. If the type is any other type, the function should return "Not an integer or float!" My answer that does not work: def distance_from_zero(d): if type(d) == int or float: return abs(d) else: return "Not an integer or float!" 回答1: You cannot use this kind of

De Morgan's Law

独自空忆成欢 提交于 2019-12-22 07:46:49
问题 I am trying to simplify the following using DeMorgan's Law: ! (x!=0 || y !=0) Does x!=0 simplify to x>0? Or am I wrong in the following: !(x>0 || y>0) !(x>0) && !(y>0) ((x<=0) && (y<=0)) Thanks. 回答1: Does x!=0 simplify to x>0? No that's not true. Because integers are signed. How to simplify : !(x!=0 || y !=0) ? Consider this rules : (second De Morgan's laws ) By 1., it implies !(x!=0 || y !=0) <=> (!(x!=0)) && (!(y != 0)) By 2., it implies (!(x!=0)) && (!(y != 0)) <=> (x == 0) && (y == 0) To

De Morgan's Law

我与影子孤独终老i 提交于 2019-12-22 07:46:30
问题 I am trying to simplify the following using DeMorgan's Law: ! (x!=0 || y !=0) Does x!=0 simplify to x>0? Or am I wrong in the following: !(x>0 || y>0) !(x>0) && !(y>0) ((x<=0) && (y<=0)) Thanks. 回答1: Does x!=0 simplify to x>0? No that's not true. Because integers are signed. How to simplify : !(x!=0 || y !=0) ? Consider this rules : (second De Morgan's laws ) By 1., it implies !(x!=0 || y !=0) <=> (!(x!=0)) && (!(y != 0)) By 2., it implies (!(x!=0)) && (!(y != 0)) <=> (x == 0) && (y == 0) To

How do I compare string and boolean in Javascript?

不羁岁月 提交于 2019-12-22 01:58:35
问题 I got the Json "false" from server. I respond as bool but it's Json so it's in browser type is String instead of bool . So if I run (!data) whenever I want to check "false" == false then they not worked. So how can I parse bool from String in JavaScript then? "true" == true and "false" == false . Then the code (!data) can check what it is [ true and false ] 回答1: I would just explicitly check for the string "true" . let data = value === "true"; Otherwise you could use JSON.parse() to convert

How do I compare string and boolean in Javascript?

一笑奈何 提交于 2019-12-22 01:54:54
问题 I got the Json "false" from server. I respond as bool but it's Json so it's in browser type is String instead of bool . So if I run (!data) whenever I want to check "false" == false then they not worked. So how can I parse bool from String in JavaScript then? "true" == true and "false" == false . Then the code (!data) can check what it is [ true and false ] 回答1: I would just explicitly check for the string "true" . let data = value === "true"; Otherwise you could use JSON.parse() to convert

Boolean assignment operators in PHP

纵饮孤独 提交于 2019-12-21 03:32:36
问题 I find myself doing this kind of thing somewhat often: $foo = true; $foo = $foo && false; // bool(false) With bitwise operators, you can use the &= and |= shorthand: $foo = 1; $foo &= 0; // int(0) Given that bitwise operations on 1 and 0 are functionally equivalent to boolean operations on true and false , we can rely on type-casting and do something like this: $foo = true; $foo &= false; // int(0) $foo = (bool)$foo; // bool(false) ...but that's pretty ugly and defeats the purpose of using a

Convert a ushort value into two byte values in C#

家住魔仙堡 提交于 2019-12-20 04:34:48
问题 How do I split a ushort into two byte variables in C#? I tried the following (package.FrameID is ushort): When I try to calculate this with paper&pencil I get the right result. Also, if FrameID is larger than a byte (so the second byte isn't zero), it works. array[0] = (byte)(0x0000000011111111 & package.FrameID); array[1] = (byte)(package.FrameID >> 8); In my case package.FrameID is 56 and the result in array[0] is 16 instead of 56. How can I fix this? 回答1: 0x0000000011111111 is not a binary

Is there a less convoluted way to compare file versions?

≯℡__Kan透↙ 提交于 2019-12-20 01:43:59
问题 I wrote a function to compare file versions between what a client currently has and the latest version of the file on a server. The client passes the "quad" (Major.Minor.Build.Private) version number as a string to the server, and then the server uses FileVersionInfo: // clientFileVersion will be in "quad" format, a la "3.1.4.1" private bool ServerFileIsNewer(string clientFileVersion, FileVersionInfo serverFile) { // Don't say I never learned nuthin' from Steve McConnell const int MAJOR_INDEX