It's "~" in C#. I belive there isn't an operator for it in vb.net. But could someone at least explain how it works so I can somehow get it to work in vb.net?
Printable View
It's "~" in C#. I belive there isn't an operator for it in vb.net. But could someone at least explain how it works so I can somehow get it to work in vb.net?
VB.NET Example . There are more if you just searched Google !;)
I had seen two of those links.... anyways, I thought there is a difference between the ~ and ! in C# (well there is, but basically they do the same thing, and that's all I wanted to know).
No, they really don't!Quote:
Originally posted by MrPolite
I had seen two of those links.... anyways, I thought there is a difference between the ~ and ! in C# (well there is, but basically they do the same thing, and that's all I wanted to know).
E.g.
if(3 && !3)
is never true.
if(3 && ~3)
is always true.
ok I'm confused :D would you care to explain?Quote:
Originally posted by CornedBee
No, they really don't!
E.g.
if(3 && !3)
is never true.
if(3 && ~3)
is always true.
! operates on boolean values.
if(3 && !3)
does this:
First, it converts 3 to bool. 3 != 0, so it gets converted to true. Next it evaluates !3. Because ! (logical NOT) operates on bool, 3 gets converted to true again. Then it is negated to false and the AND expression fails.
if(3 && ~3)
does this:
First it again converts 3 to true. The second part is different. ~ (bitwise NOT) operates not on booleans but on integers. 3 stays 3 and assuming 32-bit signed integers, the bit pattern 00000000000000000000000000000011 (3) is negated to 11111111111111111111111111111100 (-4).
-4 then gets converted to bool and results in true (-4 != 0). So the AND expression succeeds.
So bitwise NOT and logical NOT are logical NOT the same ;)
Umm, I'm not quite sure how that equates to -4! I assume the first bit designates the sign (+/-), but then wouldn't -4 = 10000000000000000000000000000100?Quote:
Originally posted by CornedBee
11111111111111111111111111111100 (-4).
-4 then gets converted to bool and results in true (-4 != 0). So the AND expression succeeds.
EDIT:
Never mind! I figured it out!