gpt4 book ai didi

vb.net - 如何计算表达式 True And -1?

转载 作者:行者123 更新时间:2023-12-01 12:32:05 25 4
gpt4 key购买 nike

为什么表达式 True And -1 的计算结果为 -1

我希望第二个操作数(即 -1)会自动转换为 True,结果将为 True

我意识到 And 在这里被解释为按位 AND 运算符。但是,当第一个操作数是 bool 值并且可以进行逻辑解释时,为什么会这样呢?

最佳答案

这就是 And Operator 的方式应该工作。引用文档:

If the operands consist of one Boolean expression and one numeric expression, Visual Basic converts the Boolean expression to a numeric value (–1 for True and 0 for False) and performs a bitwise operation.

为什么要这样做?我们只能推测,但我怀疑有两个原因:

  1. 向后兼容。 VB6 和 VBA 的行为也是如此。

  2. 对于逻辑运算,应该改用新的短路运算符AndAlsoOrElse。因此,遗留运算符 AndOr 主要用于按位运算是有道理的。

关于vb.net - 如何计算表达式 True And -1?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32353959/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com