PDA

Click to See Complete Forum and Search --> : bitwise Not in vb.net?


MrPolite
May 24th, 2003, 07:41 PM
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?

Pirate
May 25th, 2003, 02:59 AM
~ Operator (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrf~operator.asp)
Bitwise/Logical operators : How it works ? (http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=610)

Pirate
May 25th, 2003, 05:40 AM
VB.NET Example (http://visualbasic.about.com/library/bldykand_or_nota.htm) . There are more if you just searched Google !;)

MrPolite
May 25th, 2003, 03:53 PM
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).

CornedBee
May 26th, 2003, 02:51 AM
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).

No, they really don't!

E.g.

if(3 && !3)
is never true.

if(3 && ~3)
is always true.

MrPolite
May 26th, 2003, 03:10 AM
Originally posted by CornedBee
No, they really don't!

E.g.

if(3 && !3)
is never true.

if(3 && ~3)
is always true. ok I'm confused :D would you care to explain?

CornedBee
May 26th, 2003, 09:35 AM
! 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 ;)

Hu Flung Dung
May 26th, 2003, 02:06 PM
Originally posted by CornedBee
11111111111111111111111111111100 (-4).
-4 then gets converted to bool and results in true (-4 != 0). So the AND expression succeeds.


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?



EDIT:

Never mind! I figured it out!