Solved

And operator with a boolean and a hex value

Posted on 2004-03-30
8
483 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

777 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