Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is -1 the result of coercing True to an integer in 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.

like image 653
Cameron Avatar asked Oct 19 '10 23:10

Cameron


People also ask

Is true the same as 1?

These are distinct from numeric values, so true is not equal to 1, and false is not equal to 0.

What is the value of true in integer?

True is stored as -1 and false as 0. Any non-zero value is considered as true.

What is the integer value of the Boolean value true '?

Boolean values and operations Constant true is 1 and constant false is 0. It is considered good practice, though, to write true and false in your program for boolean values rather than 1 and 0. The following table shows comparisons and boolean operations.

What is numeric value of True and false?

All data has a logical (or Boolean) value, which is to say that it can be computed as true or false. If the data contains only numeric values and the numeric value is zero (0), it is false; any other numeric value is true.


2 Answers

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...

like image 71
Shog9 Avatar answered Sep 28 '22 08:09

Shog9


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.

like image 20
Chris Smith Avatar answered Sep 28 '22 07:09

Chris Smith