Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is True equal to -1

Tags:

vb.net

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()
like image 563
the_lotus Avatar asked Jan 22 '13 15:01

the_lotus


People also ask

What is the difference between 1 == true and 2 == true?

@user193130: Sorry, that was a mistake in the answer, when you compare them, the conversion is from boolean to number. The true is converted to 1, so 1 == true evaluates to true, while 2 == true evaluates to false. When you use a value as a condition, the conversion has to be to boolean, because that is the only type that a condition can be.

What is the difference between == equality and === type equality?

The == equality operator happily converts between types to find a match, so 1 == true evaluates to true because true is converted to 1. The === type equality operator doesn't do type conversions, so 1 === true evaluates to false because the values are of different types.

Does -1 equal true in JavaScript?

And I think it is pretty confusing, that -1 equals true in JavaScript! For example if you do if (str.indexOf ("something")) { dosomething (); } It does even when "something" is not found in str variable. @McVitas: The indexOf method isn't meant to be used that way.

Is it true that true and false don't represent any numerical values?

It's true that true and false don't represent any numerical values in Javascript. In some languages (e.g. C, VB), the boolean values are defined as actual numerical values, so they are just different names for 1 and 0 (or -1 and 0). In some other languages (e.g. Pascal, C#), there is a distinct boolean type that is not numerical.


1 Answers

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.

like image 112
Steven Doggart Avatar answered Oct 02 '22 15:10

Steven Doggart