473,779 Members | 2,083 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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
Nov 20 '05 #1
33 2507
HI Ruffin,

Why start counting in programlanguage s 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
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

Nov 20 '05 #2
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.

"Ruffin Bailey" <ka****@mailina tor.com> wrote in message
news:fd******** *************** ***@posting.goo gle.com...
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

Nov 20 '05 #3
* ka****@mailinat or.com (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.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #4
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
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.

Nov 20 '05 #5
* "Cor Ligthert" <no**********@p lanet.nl> scripsit:
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.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #6
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

"Ruffin Bailey" <ka****@mailina tor.com> wrote in message
news:fd******** *************** ***@posting.goo gle.com...
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

Nov 20 '05 #7
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

"Ruffin Bailey" <ka****@mailina tor.com> wrote in message
news:fd******** *************** ***@posting.goo gle.com...
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

Nov 20 '05 #8
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
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.

Nov 20 '05 #9
In the fervor of getting into the mechanics of it all, the simplicity of your
explanation eluded me.
Spot On!

Gerald

"Greg Young" <gr********@pla netbeach.com> wrote in message
news:uG******** ******@TK2MSFTN GP09.phx.gbl...
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

"Ruffin Bailey" <ka****@mailina tor.com> wrote in message
news:fd******** *************** ***@posting.goo gle.com...
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


Nov 20 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3235
by: srommens | last post by:
Hello, When I try to do : URI = <xsl:value-of select="//@URI"/><br/> <xsl:if test = "//@URI != '5-42922'">not equal</xsl:if><br/> <xsl:if test = "//@URI = '5-42922'">equal</xsl:if><br/> The result is :
46
4262
by: Scott Chapman | last post by:
There seems to be an inconsistency here: Python 2.3.2 (#1, Oct 3 2003, 19:04:58) on linux2 >>> 1 == True True >>> 3 == True False >>> if 1: print "true" ....
8
4648
by: Avin Patel | last post by:
Hi, Does the Double has the facilty to define -0. If it has, "==" or System.Math.Sign() doesn't able to differentiate between -0 & 0. i.e. Double x = -0.0d Double y = 0.0d if(value == -0.0d) {//both x & y drops in this statement.
13
5100
by: Marc | last post by:
Hi, I've been lurking on clc for a few months now, and want to start by thanking the regulars here for opening my eyes to a whole new dimension of "knowing c". Considering I had never even touched the standards a year ago, though I graduated in embedded SW development... Anyway, to the problem at hand: I've stumbled upon the following construct at work recently, and cannot really make up my mind about the standard's take on the matter.
30
3160
by: Jason | last post by:
I am fairly new to ASP--I have been using it about 2 months. I did these tests (below), and it doesn't make sense to me. False is equal to 0, and that's fine. True should be equal to 1, but it's not. Actually, True should be equal to anything but False, null, and 0. Is there a workaround for this? Or do I need to change all my comparisons to = 1 instead of = true? response.write True = 1 'prints False response.write True = 0 ...
2
3161
by: Peter Vestergaard | last post by:
Hi I have a project with quite a number of dialogs in it all with Localizable=true, and all except one working fine. But the one is causing me trouble because even though Localizable=true is set, all strings (.Text properties and the like) are still written to InitializeComponent() in the .cs file instead of to the .resx file. I have tried setting the Localizable to false again, saving/building/restarting studio, and then setting...
90
3465
by: John Salerno | last post by:
I'm a little confused. Why doesn't s evaluate to True in the first part, but it does in the second? Is the first statement something different? False print 'hi' hi Thanks.
11
2675
by: Bernard.Mangay | last post by:
The remainder is non zero due to rounding errors. How can I remove the rounding errors?
0
9636
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10306
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10139
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10075
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8961
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5373
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5504
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4037
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3632
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.