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

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

问题描述

我正在使用用 VB 编写的遗留代码库,遇到了一个我不理解的条件运算符,无法弄清楚要搜索什么来解决它.

我正在处理的是以下代码和结果为 true 的变量.我不明白的具体部分是(1)第一个X和第一个parens (-2的关系和(2)X的作用<2

  • 如果 X 是低于 -2 的值,则计算结果为 false.
  • 如果 X 是高于 2 的值,则计算结果为 true.
  • 如果 Y 低于 5 则结果为 true 符合预期.
<小时>

X = 3Y = 10如果 X >(-2 且 X <2) 或 Y <5 然后'真的别的'错误的万一

解决方案

我要离开 Or Y <5 对这篇文章的表达方式的一部分作为无趣,并将自己限制在 X >(-2 And X < 2) 表达式的一侧.

在过去的几年里我没有做太多的 VB,所以我开始研究 VB 中的运算符优先规则,以确保我做对了.我找到了有关 VBA 和 VB.Net 的权威信息,以及一些可能是 VB6 但也可能是 2013 版 VB.Net 的 MSDN 资料.但是,所有这些都赋予 <> 比较运算符高于 And 运算符的优先级,无论您是否看到 And 按逻辑或按位.

有了这些信息,并且知道我们必须首先查看括号内的内容,我现在确信要评估的表达式的第一部分是 X <2(而不是 -2 和 X).此外,我们知道这将产生一个布尔结果,然后这个布尔结果必须被转换为一个整数来执行 bitwise(not 逻辑)And-2.这个结果(我称之为n)仍然是一个整数,最后可以比较看看是否X>n,它将产生表达式的最终结果为布尔值.

我做了更多的挖掘,发现 this Stack Overflow answer 关于将 VB 布尔值转换为整数.虽然不是最终的文档,但我有幸见到了作者(嗨@JaredPar)并且知道他在微软的 VB 编译器团队工作,所以他应该知道他在说什么.它表明 VB Boolean True 具有 -1 作为整数的惊人值!False 变成更正常的 0.

此时我们需要谈谈负数的二进制表示.使用 此参考 作为一个指南(我依稀记得在大学里学过这个,但这不是我每天都需要的那种东西),我将提供一个从 -3 到 +3 的整数的转换表,其假想整数大小仅为4 位(短版:反转位模式并加一以获得负表示):

<上一页>-3 1101-2 1110-1 1111 -- 这有助于解释**为什么** -1 用于 True0 00001 00012 00103 0010

退一步,现在让我们考虑一下原来的 -2 和 X <2 括号并查看 X < 的 True (-1) 和 False (0) 可能结果的结果.2 后按位 And-2:

<上一页>-2 (1110) 和真 (1111) = 1110 = -2-2 (1110) 和假 (0000) = 0000 = 0

真正的重点是使用 -1 位模式 anything And True 产生与您开始时相同的东西,而任何东西And False 产生全零.

所以如果 X <2 你得到True,结果是-2;否则你会得到 0.有趣的是,如果我们的语言对 True 使用正数,那么您最终会得到相同的 0000 值,按位执行 And使用您从 False (1110 And 0001) 获得的 -2.

现在我们知道的足够多,可以查看 X 的一些值并确定整个原始表达式的结果.任何正整数都大于 -20,因此表达式的结果应为 True.0 和 -1 是相似的:它们小于 2,因此只会在大于 -2 时再次进行比较,因此总是导致 True.但是,负二以及下面的任何内容都应该是 False.

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

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.

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

  • 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

解决方案

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.

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.

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.

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.

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

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

Really the whole point here from using the -1 bit pattern is anything And True produces that same thing you started with, whereas anything And False produces all zeros.

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).

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.

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天全站免登陆