VB.NET中令人困惑的逻辑运算符 [英] Confusing Logical Operators in VB.NET

查看:210
本文介绍了VB.NET中令人困惑的逻辑运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用VB编写的遗留代码库,遇到了我不理解的条件运算符,无法确定要查找的内容来解决它.

I am working with a legacy code base written in VB and have run into a conditional operator that I don't understand and cannot figure out what to search for to resolve it.

我要处理的是以下代码和结果为true的变量.我不理解的具体部分是(1)第一个X和第一个括号(-2之间的关系,以及(2)X < 2

What I am dealing with is the following code and the variables that result as true. The specific parts that I do not understand are (1) the relationship between the first X and the first parens (-2 and (2) the role of X < 2

  • 如果X的值小于-2,则其值为false.
  • 如果X的值大于2,则其值为true.
  • 如果Y低于5,则其评估结果将为true.
  • If X is a value below -2 it evaluates as false.
  • If X is a value above 2 it evaluates as true.
  • If Y is below 5 it evaluates to true as expected.
X = 3
Y = 10

If X > (-2 And X < 2) Or Y < 5 Then
    'True
Else
    'False
End If

推荐答案

对于这篇文章,我将不去表达该表达式的Or Y < 5部分,而是将自己限制在该表达式的X > (-2 And X < 2)一侧.

I'm gonna leave off the Or Y < 5 part of the expression for this post as uninteresting, and limit myself to the X > (-2 And X < 2) side of the expression.

在过去的几年中,我并没有做太多的VB工作,因此我从VB中的操作员优先"规则入手,以确保我做对了事.我在VBA和VB.Net上找到了明确的信息,以及一些可能是VB6但也可能是VB.Net的2013版本的MSDN内容.但是,无论您将And是逻辑还是按位运算,所有这些运算符都赋予<>比较运算符比And运算符更高的优先级.

I haven't done much VB over the last several years, so I started out with some digging into Operator Precedence rules in VB, to be sure I have things right. I found definitive info on VBA and VB.Net, and some MSDN stuff that might have been VB6 but could also have been the 2013 version of VB.Net. All of them, though, gave the < and > comparison operators higher precedence over the And operator, regardless of whether you see And as logical or bitwise.

有了该信息,并且还知道我们必须首先在括号内查找,所以我现在确定要计算的表达式的第一部分是X < 2(而不是-2 And X).此外,我们知道这将产生布尔结果,然后必须将该布尔结果转换为Integer以对进行 (逻辑)And >.最后仍然可以比较该结果(我称它为n),以查看是否X > n可以将表达式的最终结果生成为布尔值.

With that info, and also knowing that we must look inside parentheses first, I'm now confident the very first part of the expression to be evaluated is X < 2 (rather than -2 And X). Further, we know this will produce a Boolean result, and this Boolean result must be then converted to an Integer to do a bitwise (not logical) And with -2. That result (I'll call it n), which is still an Integer, can at last be compared to see if X > n, which will yield the final result of the expression as a Boolean.

我做了一些进一步的挖掘,发现了此Stack Overflow答案有关将VB布尔值转换为整数的信息.尽管不是权威性的文档,但我曾经有幸见到作者(您好,@ JaredPar),并且知道他在Microsoft的VB编译器团队中工作,所以他应该知道他在说什么.它表明VB布尔值True具有令人惊讶的值-1作为整数! False变为更普通的0.

I did some more digging and found this Stack Overflow answer about converting VB Booleans to Integers. While not definitive documentation, I was once privileged to meet the author (Hi @JaredPar) and know he worked on the VB compiler team at Microsoft, so he should know what he's talking about. It indicates that VB Boolean True has the surprising value of -1 as an integer! False becomes the more-normal 0.

在这一点上,我们需要讨论负数的二进制表示形式.使用此参考作为指南(我确实记得在大学期间对此有所了解,但这不是我每天都需要的东西),我将提供一个从-3到+3的整数的转换表,虚数大小仅为4位(简短版本:将位模式反转并加1以获得否定表示):

At this point we need to talk about the binary representation of negative numbers. Using this reference as a guide (I do vaguely remember learning about this in college, but it's not the kind of thing I need every day), I'm going to provide a conversion table for integers from -3 to +3 in an imaginary integer size of only 4 bits (short version: invert the bit pattern and add one to get the negative representation):


-3   1101
-2   1110
-1   1111 --this helps explain **why** -1 was used for True
 0   0000
 1   0001
 2   0010
 3   0010

退后一步,现在让我们考虑原始的-2 And X < 2括号,并在对And-2进行按位And之后,查看X < 2可能为True(-1)和False(0)的结果: /p>

Stepping back, let's now consider the original -2 And X < 2 parenthetical and look at the results from the True (-1) and False (0) possible outcomes for X < 2 after a bitwise And with -2:


-2 (1110) And True  (1111) = 1110 = -2
-2 (1110) And False (0000) = 0000 =  0

因此,如果X < 2,您得到True,则结果为-2;否则,您将得到0.有趣的是,如果我们的语言对True使用正数,您将得到与0000相同的0000值,对False进行逐位AndFalse的匹配(1110 And 0001 ).

So if X < 2 you get True, which results in -2; otherwise you end up with 0. It's interesting to note here that if our language used positive one for True, you'd end up with the same 0000 value doing a bitwise And with -2 that you get from False (1110 And 0001).

现在,我们足够了解X的某些值并确定整个原始表达式的结果.任何正整数都大于-20,因此表达式的结果应为True.零和-1相似:它们小于2,因此仅在大于-2时再次进行比较,因此始终得出True.但是,负两个以及下面的任何内容都应为False.

Now we know enough to look at some values for X and determine the result of the entire original expression. Any positive integer is greater than both -2 and 0, so the expression should result in True. Zero and -1 are similar: they are less than two, and so will be compared again only as greater than -2 and thus always result in True. Negative two, though, and anything below, should be False.

不幸的是,这意味着您可以将整个表达式简化为X > -2.我对运算符优先级的理解是错误的,我对负整数位模式的引用是错误的,或者您正在使用将True转换为-1以外的其他版本的VB,或者此代码只是 way 从一开始就变得过于复杂.

Unfortunately, this means you could simplify the entire expression down to X > -2. Either I'm wrong about operator precedence, my reference for negative integer bit patterns is wrong, you're using a version of VB that converts True to something other than -1, or this code is just way over-complicated from the get-go.

这篇关于VB.NET中令人困惑的逻辑运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆