为什么 True 等于 -1 [英] Why is True equal to -1
问题描述
我想知道为什么 True 等于 -1 而不是 1.如果我没记错的话(回到过去)在 C 中,true"将等于 1.
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.
Dim t, f As Integer
t = True
f = False
Console.WriteLine(t) ' -1
Console.WriteLine(f) ' 0
Console.ReadLine()
推荐答案
当您将任何非零数字转换为 Boolean
时,它的计算结果为 True
.例如:
When you cast any non-zero number to a Boolean
, it will evaluate to True
. For instance:
Dim value As Boolean = CBool(-1) ' True
Dim value1 As Boolean = CBool(1) ' True
Dim value2 As Boolean = CBool(0) ' False
然而,正如您所指出的,任何时候您将设置为 True
的 Boolean
转换为 Integer
,它的计算结果为-1,例如:
However, as you point out, any time you cast a Boolean
that is set to True
to an Integer
, it will evaluate to -1, for instance:
Dim value As Integer = CInt(CBool(1)) ' -1
这是因为 -1
是所有位都等于 1 的有符号整数值.由于 Boolean
存储为 16-位整数,通过简单地不处理所有位而不是只不处理最不重要的位,更容易在真和假状态之间切换.换句话说,为了使 True
成为 1
,它必须像这样存储:
The reason for this is because -1
is the signed-integer value where all of its bits are equal to 1. Since a Boolean
is 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 True
to be 1
, it would have to be stored like this:
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)
鉴于:
0000000000000001 <> NOT(0000000000000000)
0000000000000001 = NOT(1111111111111110)
例如,您可以使用像这样的 Int16
变量来复制这种行为:
For instance, you can replicate this behavior using Int16
variables like this:
Dim value As Int16 = 0
Dim value2 As Int16 = Not value
Console.WriteLine(value2) ' -1
如果您使用无符号整数,这一点会更明显,因为那样的话,True
的值是最大值而不是 -1.例如:
This would be more obvious if you were using unsigned integers, because then, the value of True
is the maximum value rather than -1. For instance:
Dim value As UInt16 = CType(True, UInt16) ' 65535
那么,真正的问题是,为什么 VB.NET 使用 16 位来存储单个位值.真正的原因是速度.是的,它使用了 16 倍的内存,但处理器执行 16 位布尔运算的速度比执行单位布尔运算快得多.
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.
旁注:-1
的 Int16
值存储为 11111111111111111
而不是 1000000000000001
的原因>,如您所料(其中第一位是符号位",其余的将是值),因为它存储为二进制补码.将负数存储为二进制补码意味着处理器执行算术运算要容易得多.它也更安全,因为使用两个赞美,没有办法将 0
表示为负数,这可能会导致各种混乱和错误.
Side note: The reason why the Int16
value of -1
is stored as 1111111111111111
instead 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 0
as a negative number, which could cause all sorts of confusion and bugs.
这篇关于为什么 True 等于 -1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!