为什么在 VB6 中将 True 强制转换为整数的结果是 -1?

发布于 2024-09-27 20:47:48 字数 414 浏览 3 评论 0原文

在 VB6 中,将 True 强制转换为整数会产生值 -1。

为什么会这样呢?这背后的原因是什么?

在大多数其他编程语言(C/C++、Java、Perl、Python 等)中,true 在强制转换为整数时变为 1。在布尔代数中,值 1 用于表示 true/on。为什么 VB6 的做法有所不同?

我确实看到了某种优雅的对称性,即 -1(True)的按位非将产生 0(False),反之亦然(因为 -1 的表示在 二进制补码),但我想不出这个身份有什么实际好处。

顺便说一句,我只是出于好奇而问——当我第一次学习 VB6 时,这让我感到很奇怪,从那时起我就一直想知道。

In VB6, coercing True to an integer yields the value -1.

Why is this so? What is the reasoning behind this?

In most other programming languages (C/C++, Java, Perl, Python, etc.), true becomes 1 when coerced into an integer. In boolean algebra, the value 1 is used to represent true/on. Why does VB6 do it differently?

I do see a certain elegant symmetry in the fact that a bitwise-not of -1 (True) will yield 0 (False), and vice-versa (because of -1's representation being all 1s in two's complement), but I can't think of any practical benefits of this identity.

I'm only asking out of curiosity, by the way -- this was something that struck me as odd when I first learnt VB6, and I've been wondering ever since.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

很酷不放纵 2024-10-04 20:47:48

您非常接近原因... Eric Lippert 揭示了可怕的、可怕的真相

发生的事情是 VBScript 不符合逻辑。 VBScript 是按位的。所有所谓的逻辑运算符都适用于数字,而不是布尔值! Not、And、Or、XOr、Eqv 和 Imp 都将其参数转换为四字节整数,对整数中的每对位进行逻辑运算,然后返回结果。如果 True 为 -1,False 为 0,则一切正常,因为 -1 的所有位均打开,而 0 的所有位均关闭。

(如 Chris Smith 指出,长期以来,各种 BASIC 风格都是如此……)

请注意,在 VB.NET 中,逻辑运算符(即操作的运算符引入了布尔数据类型,但如果输入整数类型,现有运算符仍然会愉快地执行按位运算。当我在 C++ 和 VB 之间转换时,这经常让我感到沮丧……

You came very close to the reason... Eric Lippert reveals the horrible, horrible truth:

What's going on is that VBScript is not logical. VBScript is bitwise. All the so-called logical operators work on numbers, not on Boolean values! Not, And, Or, XOr, Eqv and Imp all convert their arguments to four-byte integers, do the logical operation on each pair of bits in the integers, and return the result. If True is -1 and False is 0 then everything works out, because -1 has all its bits turned on and 0 has all its bits turned off.

(As Chris Smith notes, this has been true of various flavors of BASIC for a long time...)

Note that in VB.NET, logical operators (that is, operators that operate only on Boolean datatypes) were introduced, but the existing operators will still happily perform bitwise operations if fed integral types. This has been a frequent source of frustration for me when moving between C++ and VB...

神经大条 2024-10-04 20:47:48

长期以来,在 Microsoft basic 中都是如此,至少 GW-BASIC 是这样。我认为这是因为当时没有实际的布尔类型或单独的逻辑 NOT 运算符,因此为了使整数 NOT 运算符同时适用于 true 和 false,他们使用了值 0 和 -1。正 1 不起作用,因为 NOT 1 不为零。

This is been true in Microsoft basic for a long time, GW-BASIC at least. I think it's because there was no actual boolean type in those days or a separate logical NOT operator, so to make the integer NOT operator work with both true and false, they used values 0 and -1. Positive 1 wouldn't have worked because NOT 1 is not zero.

中性美 2024-10-04 20:47:48

True 仅表示为二进制(11111111 等),False 表示仅零(00000 等)

数据类型 int 有符号,这意味着当 0 时,最高有效位告诉我们该值为正数,当为 1 时,最高有效位告诉我们该值为负数。尝试将最大的、可能的正数溢出 1,然后它将“翻转”到可能的最大的负数。

十进制 -1 仅表示为 1。给你了:)

True is represented as only binary ones (11111111 etc..), false as only zeros (00000 etc..)

The datatype int is signed, that means the msb when zero tells us that the value is positive, and when one that it's negative. Try to overflow the largest, possible, positive number by one, it will then "roll over" to the largest, negative number possible.

Decimal -1 is represented as only ones. There you have it :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文