Using bitwise operators

后端 未结 12 1736
既然无缘
既然无缘 2021-01-06 08:21

I\'ve been studying C# and ran accross some familiar ground from my old work in C++. I never understood the reason for bitwise operators in a real application. I\'ve never u

12条回答
  •  情深已故
    2021-01-06 08:52

    A typical use is manipulating bits that represent mutually exclusive 'flags'.

    Example from MSDN: Enumeration Types

    [Flags]
    enum Days2
    {
        None = 0x0,
        Sunday = 0x1,
        Monday = 0x2,
        Tuesday = 0x4,
        Wednesday = 0x8,
        Thursday = 0x10,
        Friday = 0x20,
        Saturday = 0x40
    }
    
    class MyClass
    {
        Days2 meetingDays = Days2.Tuesday | Days2.Thursday;
    
        Days2 notWednesday = ~(Days2.Wednesday);
    }
    

    See also Stack Overflow question Most common C# bitwise operations.

提交回复
热议问题