52

Possible Duplicate:
What is the tilde (~) in a C# enumeration?

I found the following bit of code on a this MSDN page.

(((Width * Planes * BitCount + 31) & ~31) / 8) * abs(Height)

This does indeed compile in C# visual studio 2010. What exactly is the tilde "~" doing in front of the number 31? I've never seen this syntax in an expression before.

Community
  • 1
  • 1
Ultratrunks
  • 2,464
  • 5
  • 28
  • 48
  • 2
    Its the bitwise complement. See the below link. [How Does the Bitwise Complement Operator Work][1] [1]: http://stackoverflow.com/questions/791328/how-does-the-bitwise-complement-operator-work – Nerdtron Nov 02 '11 at 20:36
  • For future ref here's the list of the c# operators. [C# Operators](http://msdn.microsoft.com/en-us/library/6a71f45d.aspx) – Conrad Frix Nov 02 '11 at 20:38

6 Answers6

71

~ - bitwise NOT operator, basically invert bits

31 in binary format is 11111, so ~31 == 11111111111111111111111111100000, or 0xFFFFFFE0 hexadecimal.

John Cummings
  • 1,949
  • 3
  • 22
  • 38
sll
  • 61,540
  • 22
  • 104
  • 156
  • +1 as this is the easiest to understand and clearest answer. – James Hatton Oct 14 '14 at 19:48
  • 14
    The example is not correct, because leading zero are also inverted. I.e. `~31 = ~0x0000001F = 0xFFFFFFE0` – JimiLoe Feb 29 '16 at 22:42
  • 4
    this example is not correct 31's two's complement binary is 00000000000000000000000000011111 (which is 0000001F in Hex - if 32bit is being calculated) and "~" operator flips the bits, so the answer will be 11111111111111111111111111100000, if we convert this to decimal, answer is -32 If 64bit, 31's two's complement binary is 0000000000000000000000000000000000000000000000000000000000011111 and "~" flips the bits, which is equivalent again to -32. AFAIK there is no 6bit PC that runs C# that why yours is wrong. If there is a 6 bit PC, this is correct. – Isham Mohamed Oct 13 '17 at 14:05
39

That is the bitwise complement operator, also known as bitwise negation.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
11

It is the bitwise complement operator.

Basically, it flips the bits:

0xffff0000 == ~0x0000ffff

In the code you have posted, doing & ~31 ensures the last 5 bits are 0 (bitwise and of the complement of 11111 which is 00000).

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

You turn to your handy copy of ISO/IEC 23270:2006 — Information technology — Programming languages — C# and turn to §14.6.4 of the holy write. There you will find:


14.6.4 Bitwise complement operator

For an operation of the form ~x, unary operator overload resolution (§14.2.3) is applied to select a specific operator implementation. The operand is converted to the parameter type of the selected operator, and the type of the result is the return type of the operator. The predefined bitwise complement operators are:

int   operator ~( int   x ) ;
uint  operator ~( uint  x ) ;
long  operator ~( long  x ) ;
ulong operator ~( ulong x ) ;

For each of these operators, the result of the operation is the bitwise complement of x.

Every enumeration type E implicitly provides the following bitwise complement operator:

E operator ~(E x);

The result of evaluating ~x, where x is an expression of an enumeration type E with an underlying type U, is exactly the same as evaluating unchecked((E)(~(U)x)). This operator is only considered by unary operator overload resolution when the operand type is the enum type E (§14.2.3).

Lifted (§14.2.7) forms of the unlifted predefined bitwise complement operators defined above are also predefined.


In your case ~31 is the same as ~ 0x0000001F. The bitwise complenent of 0x0000001F is 0xFFFFFFE0. Why they wouldn't just write the actual mask they wanted is beyond me.

Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135
1

This is the bitwise complement operator - it just makes all 0 bits into 1s and vice versa... see MSDN reference.

In your specific case it just creates (31 = 0x1F):

~0x1F = 0xFFFFFFE0

It is used with the bitwise and (&) and thus bascially it cancels out the last 5 bits.

Yahia
  • 69,653
  • 9
  • 115
  • 144
0

~31 = bitwise negation of 31 and in this particular case is used to keep to zero the first (from LSB ) 5 bits of (Width * Planes * BitCount + 31)

Felice Pollano
  • 32,832
  • 9
  • 75
  • 115