Why is minus one (-1) equal to true in VB again?

R

Ruffin Bailey

I coulda sworn I was given an explanation during an AppDev class years
ago for VB6, but don't recall the answer. Why is it that -1 is True
in Visual Basic (and now VB.NET)? Bit flags seem like they should
always be 0 or 1 to me... (not that I haven't used VB long enough by
now to know better).

Sorry to pester, but "why is -1 = true?" is a difficult thing to
Google!

Ruffin Bailey
 
C

Cor Ligthert

HI Ruffin,

Why start counting in programlanguages with zero and in normal counting with
one. Just because it is terrible difficult to change those things I assume
and keep it backwards compatible.

(I think that when the counting would start at one it was also easier to set
the false to zero).

Just my thought.

Cor
 
M

Marina

I don't have an answer for you, but I do have an opinion. And that is that
you should turn option strict on, and that way something like
If -1 = True Then

Would never even compile. You shouldn't be coding relying on late binding to
turn integers into booleans, etc.
 
H

Herfried K. Wagner [MVP]

* (e-mail address removed) (Ruffin Bailey) scripsit:
I coulda sworn I was given an explanation during an AppDev class years
ago for VB6, but don't recall the answer. Why is it that -1 is True
in Visual Basic (and now VB.NET)? Bit flags seem like they should
always be 0 or 1 to me... (not that I haven't used VB long enough by
now to know better).

Booleans are stored as 32-bit-integers.

'False' = 000000....000 (BIN) = 0 (DEC)
'True' = 111111....111 (BIN) = -1 (DEC)

The first bit is the sign bit, if it's set to 1 that indicates a
negative number.
 
C

Cor Ligthert

Hi Herfried,

This is how it is done for a boolean you need only one bulb which can be on
and off.
(or one bit in a computer)

:)

Cor
 
H

Herfried K. Wagner [MVP]

* "Cor Ligthert said:
This is how it is done for a boolean you need only one bulb which can be on
and off.
(or one bit in a computer)

:)

Sure, but we are running a 32 bit computer, that's why a 'Boolean' is 32
bits. Building the complement of all bits is a very easy operation, so
there is no need to play around with a single bit.
 
G

Greg Young

booleans have as far as I now always been defined as

FALSE = 0
TRUE = !FALSE

so it could be -1,27 1, or any other arbitrary value when stored in a field
larger than 1 bit.

Greg
 
C

Cablewizard

wow, this one has a long history. let's see if I can recall the explanation from
my machine language days...
It basically comes down to a number of things, many historical.
In certain situations in Boolean math (also two's complement), the setting or
clearing of all bits would represent an absolute True or False.
Various hardware, processors, etc. have differing native digits of Integer
precision. At the time, it was 4, 8, or 16. Obviously this has changed with
time, and will no doubt continue to change.
Cross platform code works fastest when the values can be manipulated in the
native precision of the processor registers.
There is no standard 1 Bit data type. Especially in VB.
For normal signed integers, the high bit evaluates as the Sign of the integer.
IF you did have a 1 Bit signed integer type, if the value was not 0, it would
be -1 (or -0, but that's a discussion for another day)
If you carry that over to integers of any arbitrary precision, it would make
sense to only need to look at a common register flag. In this case, the Sign
bit. If you needed to convert native integer precision across platforms, this
would require unnecessary casting of values, which would be inefficient.
Besides, casting a non zero 1 Bit integer to any other integer would still
result in -1. Same is the case if you cast a -1 Int32 to an Int64, still = -1.
Note I am talking about Casting, not a Bit-wise compare.
Additionally, by storing the Boolean value as all 1's or all 0's, you get an
additional performance gain when dealing with IO, as you can ignore the Byte
Ordering (Little/Middle/Big Endean). 1111 = 1111 forward or backward.
A Boolean evaluation of an expression is actually a double negative.
If an expression does not evaluate to False(0), then it must be True. So all non
zero values are True.
Basically, given VAR=1 then the statement
"IF VAR THEN"
would really be more like
"IF (VAR <> 0) [returns True {-1}] THEN"
Low level language programmers used to take advantage of this to save a couple
chars in the source when you only had a few KB of space if you were lucky.

Since there is no Bit data type, you can't pass a Bit value as a parameter into
or out of a function, it would still need to be cast to the smallest native data
type supported.
Since VB was designed for 32 bit W/Intel systems, it only makes sense to use the
native data type of the processor for the sake of speed, memory storage, etc. In
this case, that happens to be a 32 Bit Signed Integer. Note that even though
there is a Byte data type, when it is passed to the registers it still goes in
as an Int32.

Sorry if that explanation meandered a little. But hopefully it helps to explain
the "why".

Gerald
 
C

Cor Ligthert

Hi Herfried,

You do not believe it, ;-) I was already ready for that answer from you,
however that it is technical more easy to read a whole 32 bit word in a
register than a single bit and with that set the bulb to on, does not answer
why it is logical -1.

:)

Cor
 
C

Cablewizard

In the fervor of getting into the mechanics of it all, the simplicity of your
explanation eluded me.
Spot On!

Gerald
 
O

One Handed Man \( OHM - Terry Burns \)

Acording to the help file . . .

Boolean variables are stored as 16-bit (2-byte) numbers

HTH

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .
 
H

Herfried K. Wagner [MVP]

* "One Handed Man \( OHM - Terry Burns \) said:
Acording to the help file . . .

Boolean variables are stored as 16-bit (2-byte) numbers

You are right ;-).
 
O

One Handed Man \( OHM - Terry Burns \)

This code disassembly appears to store the value '0' for False, if you
assign True to 'v' then the ldc.i4 line becomes ldc.i4.1.

So it would appear that in actual fact the binary storage for this type is
actually False=0 and True=1.

This seems to be converted differently when expressing as numeric types.

// Code size 5 (0x5)
.maxstack 1
.locals init ([0] bool v)
IL_0000: nop
IL_0001: ldc.i4.0
IL_0002: stloc.0
IL_0003: nop
IL_0004: ret
} // end of method Form1::Button1_Click

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .
 
M

Mythran

Think about it...when you use numbers in VB w/o conversion, what data type is 1?
Integer. So, how many bytes does an integer use? In this case, you have an
integer value so converting that to binary as Herfried described now, because of
the compliment, also turns on the sign bit. Understand???

Mythran
 
O

One Handed Man \( OHM - Terry Burns \)

This code disassembly appears to store the value '0' for False, if you
assign True to 'v' then the ldc.i4 line becomes ldc.i4.1.

So it would appear that in actual fact the binary storage for this type is
actually False=0 and True=1.This code disassembly appears to store the
value '0' for False, if you

This seems to be converted differently when expressing as numeric types.

// Code size 5 (0x5)
.maxstack 1
.locals init ([0] bool v)
IL_0000: nop
IL_0001: ldc.i4.0
IL_0002: stloc.0
IL_0003: nop
IL_0004: ret
} // end of method Form1::Button1_Click
 
C

Cor Ligthert

Hi OHM,

The correct answer is in my opinion given by Greg Young.

What you than take as value is not important as long as true not is 0.

Cor
 
H

Herfried K. Wagner [MVP]

* "One Handed Man \( OHM - Terry Burns \) said:
This code disassembly appears to store the value '0' for False, if you
assign True to 'v' then the ldc.i4 line becomes ldc.i4.1.

So it would appear that in actual fact the binary storage for this type is
actually False=0 and True=1.

This seems to be converted differently when expressing as numeric types.

// Code size 5 (0x5)
.maxstack 1
.locals init ([0] bool v)
IL_0000: nop
IL_0001: ldc.i4.0
IL_0002: stloc.0
IL_0003: nop
IL_0004: ret
} // end of method Form1::Button1_Click

Sure, 'False' is always 0, and 'True' is something <> 0. When doing a
'Not' on a 'Boolean' set to 'False' it's -1 because that's the complement of 0
interpreted as a signed short.
 
O

One Handed Man \( OHM - Terry Burns \)

My point was that true is not stored as -1, rather that it is stored as 1.
Which is different to what every one was saying. It is only when you convert
it that it to a numeric type that it becomes Minus 1.
 
H

Herfried K. Wagner [MVP]

* "One Handed Man \( OHM - Terry Burns \) said:
My point was that true is not stored as -1, rather that it is stored
as 1.

It's stored as -1 if you do a 'Not False'.
 
L

Larry Serflaten

Ruffin Bailey said:
I coulda sworn I was given an explanation during an AppDev class years
ago for VB6, but don't recall the answer. Why is it that -1 is True
in Visual Basic (and now VB.NET)?


Because the logical operators were always bit-wise operators.

That meant And, Or, Xor, and Not, could be used on numbers
as well as boolean conditional expressions.


7 And 3 = 3

if (2>0) And (4>2) then ...


It was an industry standard that False equaled 0, but what
value would satisfy these statements;

? = Not 0

0 = Not ?


The Not operator is just a bit-wise complement operator,
as it always had been, but because of that, there is only
one value that can be plugged into the statements above.

0 is a number where all bits are set to 0, so that means
its complement is a value whose bits are all set to 1.
In 2's complement notation (the format used to store signed
values in memory) a value with all its bits set to 1 equates
to -1.

LFS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top