vb.net 为什么 True 等于 -1
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14462272/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is True equal to -1
提问by the_lotus
I was wondering why True is equal to -1 and not 1. If I remember correctly (back in the days) in C, "true" would be equal to 1.
我想知道为什么 True 等于 -1 而不是 1。如果我没记错的话(回到过去)在 C 中,“true”将等于 1。
Dim t, f As Integer
t = True
f = False
Console.WriteLine(t) ' -1
Console.WriteLine(f) ' 0
Console.ReadLine()
回答by Steven Doggart
When you cast any non-zero number to a Boolean, it will evaluate to True. For instance:
当您将任何非零数字转换为 a 时Boolean,它将评估为True。例如:
Dim value As Boolean = CBool(-1) ' True
Dim value1 As Boolean = CBool(1) ' True
Dim value2 As Boolean = CBool(0) ' False
However, as you point out, any time you cast a Booleanthat is set to Trueto an Integer, it will evaluate to -1, for instance:
但是,正如您所指出的,任何时候您将 aBoolean设置True为 an Integer,它都会评估为 -1,例如:
Dim value As Integer = CInt(CBool(1)) ' -1
The reason for this is because -1is the signed-integer value where all of its bits are equal to 1. Since a Booleanis stored as a 16-bit integer, it is easier to toggle between true and false states by simply NOT'ing all of the bits rather than only NOT'ing the least significant of the bits. In other words, in order for Trueto be 1, it would have to be stored like this:
这样做的原因是因为-1它的所有位都等于 1 的有符号整数值。由于 aBoolean存储为 16 位整数,因此通过简单的 NOT'ing all of位,而不仅仅是位中最不重要的位。换句话说,为了True是1,它必须像这样存储:
True = 0000000000000001
False = 0000000000000000
But it's easier to just store it like this:
但是像这样存储它更容易:
True = 1111111111111111
False = 0000000000000000
The reason it's easier is because, at the low-level:
它更容易的原因是,在低级别:
1111111111111111 = NOT(0000000000000000)
Whereas:
然而:
0000000000000001 <> NOT(0000000000000000)
0000000000000001 = NOT(1111111111111110)
For instance, you can replicate this behavior using Int16variables like this:
例如,您可以使用如下Int16变量复制此行为:
Dim value As Int16 = 0
Dim value2 As Int16 = Not value
Console.WriteLine(value2) ' -1
This would be more obvious if you were using unsigned integers, because then, the value of Trueis the maximum value rather than -1. For instance:
如果您使用无符号整数,这一点会更加明显,因为那样, 的值True是最大值而不是 -1。例如:
Dim value As UInt16 = CType(True, UInt16) ' 65535
So, the real question, then, is why in the world does VB.NET use 16 bits to store a single bit value. The real reason is speed. Yes, it uses 16 times the amount of memory, but a processor can do 16-bit boolean operations a lot faster than it can do single-bit boolean operations.
那么,真正的问题是,为什么 VB.NET 使用 16 位来存储单个位值。真正的原因是速度。是的,它使用了 16 倍的内存量,但是处理器执行 16 位布尔运算的速度比执行单位布尔运算快得多。
Side note: The reason why the Int16value of -1is stored as 1111111111111111instead of as 1000000000000001, as you might expect (where the first bit would be the "sign bit", and the rest would be the value), is because it is stored as the two's-complement. Storing negative numbers as the two's-complement means that arithmetic operations are much easier for the processor to perform. It's also safer because, with two's-compliment, there's no way to represent 0as a negative number, which could cause all sorts of confusion and bugs.
旁注:如您所料,将of的Int16值-1存储为 as1111111111111111而不是 as的原因1000000000000001(其中第一位是“符号位”,其余的将是值),是因为它存储为两个-补充。将负数存储为二进制补码意味着处理器执行算术运算要容易得多。它也更安全,因为用 2 的赞美,没有办法表示0为负数,这可能会导致各种混乱和错误。
回答by David Brunelle
Is most language, a numeric value of 0 is false. Everything else is considered true. If I remeber correctly, -1 is actually all bits set to 1 while 0 is all bits set to 0. I guess this is why.
是大多数语言,数值为 0 是假的。其他一切都被认为是真实的。如果我没记错的话,-1 实际上是所有位都设置为 1,而 0 是所有位都设置为 0。我想这就是原因。
回答by bonCodigo
Here is the possible duplicate: Casting a boolean to an integer returns -1 for true?
这是可能的重复:将布尔值转换为整数返回 -1 为真?
Boolean constant True has numeric value ?1. This is because the Boolean data type is stored as a 16-bit signed integer. In this construct ?1 evaluates to 16 binary 1s (the Boolean value True), and 0 as 16 0s (the Boolean value False). This is apparent when performing a Not operation on a 16 bit signed integer value 0 which will return the integer value ?1, in other words True = Not False. This inherent functionality becomes especially useful when performing logical operations on the individual bits of an integer such as And, Or, Xor and Not.[4] This definition of True is also consistent with BASIC since the early 1970s Microsoft BASIC implementation and is also related to the characteristics of CPU instructions at the time.
布尔常量 True 的数值为?1。这是因为布尔数据类型存储为 16 位有符号整数。在此构造中,?1 计算为 16 个二进制 1(布尔值 True),而 0 计算为 16 个 0(布尔值 False)。这在对 16 位有符号整数值 0 执行 Not 操作时很明显,该值将返回整数值 ?1,换句话说,True = Not False。当对整数的各个位(例如 And、Or、Xor 和 Not)执行逻辑运算时,这种固有功能变得特别有用。 [4] 这种True的定义也与1970年代早期微软BASIC实现以来的BASIC一致,也与当时CPU指令的特性有关。
回答by pete
In Visual Basic, 0is Falsewhereas any non-zero value is True. Also, per MSDN:
在 Visual Basic 中,0isFalse而任何非零值都是True。此外,根据MSDN:
When Visual Basic converts numeric data type values to Boolean, 0 becomes False and all other values become True. When Visual Basic converts Boolean values to numeric types, False becomes 0 and True becomes -1.
当 Visual Basic 将数字数据类型值转换为布尔值时,0 变为 False,所有其他值变为 True。当 Visual Basic 将布尔值转换为数字类型时,False 变为 0,True 变为 -1。
回答by ja72
I guess to goes back to assembly language where a conditional is translated to a compare cmpoperation and the zero flag (ZF) is checked. For true expressions the ZFis not raised, and for false expressions it is. Early Intel processors are like that, but I cannot remember if the Zilog Z80and the Motorola 8-bit processors had the same convention.
我想回到汇编语言,在那里将条件转换为比较cmp操作并ZF检查零标志 ( )。对于真表达式ZF不会引发,而对于假表达式则是。早期的 Intel 处理器就是这样,但我不记得 ZilogZ80和 Motorola 8 位处理器是否具有相同的约定。

