Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
812 views
in Technique[技术] by (71.8m points)

vb.net - Why is True equal to -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()
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

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)

Whereas:

0000000000000001 <> NOT(0000000000000000)
0000000000000001 = NOT(1111111111111110)

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

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

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...