Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 490
  • Last Modified:

And operator with a boolean and a hex value

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
CindyZhou
Asked:
CindyZhou
  • 3
  • 3
1 Solution
 
AzraSoundCommented:
-1 And &H400
0
 
g0rathCommented:
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
 
g0rathCommented:
oops...thinking the same thing only typed slower :)
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Z_BeeblebroxCommented:
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
 
g0rathCommented:
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
 
Z_BeeblebroxCommented:
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
 
Z_BeeblebroxCommented:
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now