Solved

And operator with a boolean and a hex value

Posted on 2004-03-30
8
482 Views
Last Modified: 2006-11-17
Hi,

I am converting some VB code to C#. There is one code line using And operator with a boolean value and a hex value, say "True And &H400&". The result of "True And &H400&" is 1024.

I originally thought that "True And &H400&" gives same result as "1 And &H400&". Apprantenly it is not. Anyone how "True And &H400&" actually works so I get 1024?

I would really appreciate any suggestion.

Cindy
0
Comment
Question by:CindyZhou
  • 3
  • 3
8 Comments
 
LVL 28

Accepted Solution

by:
AzraSound earned 50 total points
ID: 10716342
-1 And &H400
0
 
LVL 5

Expert Comment

by:g0rath
ID: 10716352
that is because TRUE from VB is actually (-1)

TRUE = (-1)
FALSE = (0)

NOT the same as most everything else....I know originally only programmed in C where True = (1)
0
 
LVL 5

Expert Comment

by:g0rath
ID: 10716371
oops...thinking the same thing only typed slower :)
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 7

Expert Comment

by:Z_Beeblebrox
ID: 10716615
Its actually quite elegant, because logically, "True And X" should always evaluate to "X", and in this case "True And &H400&" evaluates to "&H400&", or in decimal 1024. And of course false does the opposite, "False And X" evaluates always to False, or in decimal 0. And the "Or" cases work out just as nicely.

Zaphod.
0
 
LVL 5

Expert Comment

by:g0rath
ID: 10716826
in Boolean algebra

1 - TRUE
0 - FALSE

usually your applying this at the bit level.

VB is just muddying the waters with their definition of "TRUE"

TRUE AND &H400& is just a hack/shortcut for proper bit masks

TRUE AND 400h evaluates to 1 AND 1024
so
10000000000    400h or 1024d
00000000001    TRUE
=========
00000000000

when what you really mean is
10000000000    400h or 1024d
11111111111    bitmask
=========
10000000000


So MS with VB6 gave you what they thought you wanted
and not was you really meant.

Now with C# and it appears with VB.Net they are actually trying
to return to doing things semi-proper
0
 
LVL 7

Expert Comment

by:Z_Beeblebrox
ID: 10719744
Gorath:

I disagree with your statement that true equals "1". A binary representation of true would be 11111111..... for however many bits you need. When you evaluate 111111.... as a long, you get -1, thus this is the VB6 implementation, and I think it makes a lot of sense. True = all bits are 1, False = all bits are 0.

But a discussion as to how it should be isn't really that important, what is important is that in VB6, True and &H400& evaluates to &H400&. The "True and" part of the expression is redundant, as I explained in my previous post.

Zaphod.
0
 
LVL 7

Expert Comment

by:Z_Beeblebrox
ID: 11265298
AzraSound answered the question first and correctly (and rather abruptly, probably trying to beat people like g0rath :P) so his comment should be accepted as an answer.

Zaphod.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now