Solved

And operator with a boolean and a hex value

Posted on 2004-03-30
8
486 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses
Course of the Month7 days, 21 hours left to enroll

617 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