1

Possible Duplicate:
How do you return 'not uint' in C#?

Hi Everyone, I'm trying to convert the following from VB.NET to C# and I'm getting a syntax error.

VB.NET:

Dim CurrentCRC As UInt16
CurrentCRC = &HFFFF
CurrentCRC = Not CurrentCRC

C#:

UInt16 currentCRC = default(UInt16);
currentCRC = 0xFFFF;
currentCRC = !currentCRC;

The last line is giving me the syntax error of

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

Any help would be appreciated!

Community
  • 1
  • 1
Since_2008
  • 2,331
  • 8
  • 38
  • 68

3 Answers3

8

If you're looking for the bitwise NOT operator (i.e. the one that flips every bit in the value), use ~. ! is the logical NOT operator (for boolean logic).

Matt Kline
  • 10,149
  • 7
  • 50
  • 87
1

! is the boolean NOT operator, i.e., it may only be applied to booleans. If you want a bitwise NOT use ~.

Ed S.
  • 122,712
  • 22
  • 185
  • 265
1

If you want a bitwise complement, use the ~ operator.

In c#, ! is only valid for negating booleans.

Oded
  • 489,969
  • 99
  • 883
  • 1,009