Solved

And operator with a boolean and a hex value

Posted on 2004-03-30
8
481 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…

707 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

13 Experts available now in Live!

Get 1:1 Help Now