It seems like many languages, including C/C++ and Java, have both a Logical Not (which converts 0 to 1 and vice-versa) and a Bitwise Not (bitwise inversion AKA one's complement). In Visual Basic, Not
is simply bitwise, and True
has the value -1. Is there any particular reason VB was implemented this way?
If it's some vestige carried over from BASIC, then does it represent a change in thinking between 1964 (BASIC) and the time of later languages like C (1972)?
There's history, BASIC also did not have a distinction between the logical and bitwise AND and OR operator. Not
does both jobs in VB.NET, it is a logical NOT if the operand is a Boolean and a bitwise negation if the operand is numerical.
True converts to -1 primarily to allow the And
and Or
operators to produce a logical expression result. So, say, True And 2
is still true and matches True And CBool(2)
, that wouldn't work if True converts to 1. The missing short-circuiting behavior was a fairly serious deficiency in old VB versions and inspired the addition of AndAlso and OrElse.
This is just speculation, but the bit-wise AND/OR/NOT was probably chosen (in the days of 8-bit BASIC implementations) for sake of easier implementation and flexibility over zero-vs-nonzero logic.
You couldn't make the language too complicated, as this was back in the "home computer" days when CPU cycles were in kilo-hertz, and memory was in kilo-bytes, and storage devices where rare or expensive or primative (such as a paper tape reader or hastily adapted cassette recorder/player).
Dartmouth BASIC (the start of it all, c 1964, http://bitsavers.trailing-edge.com/pdf/dartmouth/BASIC_4th_Edition_Jan68.pdf), didn't even have AND/OR/NOT, not even as keywords for IF.
However, as far as BASIC from Microsoft goes, the start of that is with Altair BASIC (c 1975, https://en.wikipedia.org/wiki/Altair_BASIC). Altair BASIC has AND/OR/NOT (https://ia601600.us.archive.org/35/items/bitsavers_mitsMITSAl_6669937/MITS_AltairBASIC_1975.pdf), described as "LOGICAL AND BITWISE" (pg 27 - pg 31 of PDF). It would be with Altair BASIC that False/True mapped to 0/-1; and that the THEN clause is executed for any non-zero expression of IF. This apparently is what set the idiom for later versions.
(Side note, I have no idea if their "INTRUDER ALERT" example really worked, but it does not seem right to me.)
Anyway, as AND/OR/NOT were operators that worked on integers, it would have been straightforward work to add them to their expression parser/evaluator code. Adding additional operators for zero-vs-nonzero might have been an expensive idea given the single-digit kilobyte memory capacity at the time. Short-circuit AND/OR/NOT would involve jumps, making the expression parser/evaluator a more involved design.
Since NOT 0
is -1
(all bits set to 1, and interpreted in the two's complement sense), that in itself would necessitate -1 as the standard for True (so that the "LOGICAL" aspect would work as expected).
You'll note that VB.NET is merely keeping to that legacy idiom (as did VB6 which it replaced), but it's only an issue whenever you make it cast to Integer. But as Hans Passant has indicated, VB.NET has evolved from there.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With