Funky
Funky

Reputation: 13612

What is the C# equivalent for &H2?

can someone please advise what is the c# equivelant for this:

&H2

here's the line of code from VB:

direntEnableEntry.Properties("userAccountControl").Value = intVal And Not &H2

here's the converted code:

   direntEnableEntry.Properties["userAccountControl"].Value = (intVal & !=0x2);

Operator '!' cannot be applied to operand of type 'int'

Upvotes: 1

Views: 2633

Answers (2)

Cameron
Cameron

Reputation: 98746

intVal And Not &H2 is equivalent to the following C#:

intVal & ~0x2

You got the 0x2 hex literal translated correctly, but Not in VB.NET (when applied to a numeric value) corresponds to the bitwise ~ operator, not != (which in VB is <>, if I recall correctly).

Upvotes: 10

Jonathan Wood
Jonathan Wood

Reputation: 67193

In C#, the 0x prefix denotes hexadecimal notation. So the equivalent of VB's &H2 is 0x2.

The line in question performs a bitwise NOT on this value, and then does a bitwise AND between the result and intVal. In C#, ~ is the bitwise NOT operator. So this line would look something like the following in C#:

direntEnableEntry.Properties["userAccountControl"].Value = (intVal & ~0x2);

Note that ! (and !=) is a logical NOT, which differs from a bitwise NOT.

Upvotes: 4

Related Questions