0

I'm a VB developer currently deciphering an application written in C#. Generally I can find the answer through Google. But I'm lost on this. What is the tilde (~) used for in C#??

Here is a snip-it containing the expression:

  /// <summary>
        /// Updates the window style for the parent form.
        /// </summary>
        private void UpdateStyle()
        {
            // remove the border style
            Int32 currentStyle = Win32Api.GetWindowLong(Handle, GWLIndex.GWL_STYLE);
            if ((currentStyle & (int)(WindowStyles.WS_BORDER)) != 0)
            {
                currentStyle &= ~(int) (WindowStyles.WS_BORDER);
                Win32Api.SetWindowLong(_parentForm.Handle, GWLIndex.GWL_STYLE, currentStyle);
                Win32Api.SetWindowPos(_parentForm.Handle, (IntPtr) 0, -1, -1, -1, -1,
                                      (int) (SWPFlags.SWP_NOZORDER | SWPFlags.SWP_NOSIZE | SWPFlags.SWP_NOMOVE |
                                             SWPFlags.SWP_FRAMECHANGED | SWPFlags.SWP_NOREDRAW | SWPFlags.SWP_NOACTIVATE));
            }
        }
user1932634
  • 181
  • 12

1 Answers1

5

It's the bitwise NOT operator.

In this case the following code:

currentStyle &= ~(int) (WindowStyles.WS_BORDER);

can be interpreted as: Casting the WindowStyles.WS_BORDER enum to an int, take the inverse bits (using the ~ operator) and merge them with the value held in currentStyle using & (bitwise AND). Finally, store the result back to the currentStyle variable.

PinnyM
  • 35,165
  • 3
  • 73
  • 81