Why use the Bitwise-Shift operator for values in a C enum definition?

前端 未结 8 1226
眼角桃花
眼角桃花 2020-12-02 10:42

Apple sometimes uses the Bitwise-Shift operator in their enum definitions. For example, in the CGDirectDisplay.h file which is part of Core

相关标签:
8条回答
  • 2020-12-02 10:59

    This way you can add multiple flags together to create a "set" of flags and can then use & to find out whether any given flag is in such a set.

    You couldn't do that if it simply used incrementing numbers.

    Example:

    int flags = kCGDisplayMovedFlag | kCGDisplaySetMainFlag; // 6
    if(flags & kCGDisplayMovedFlag) {} // true
    if(flags & kCGDisplaySetModeFlag) {} // not true
    
    0 讨论(0)
  • 2020-12-02 11:03

    .. because 1<<7 looks more concise and easier to read than 01000000. Doesn't it?

    0 讨论(0)
  • 2020-12-02 11:07

    Maybe writing the values in hexadecimal (or binary) helps :-)

    enum {
      kCGDisplayBeginConfigurationFlag  = (1 << 0), /* 0b0000000000000001 */
      kCGDisplayMovedFlag               = (1 << 1), /* 0b0000000000000010 */
      kCGDisplaySetMainFlag             = (1 << 2), /* 0b0000000000000100 */
      kCGDisplaySetModeFlag             = (1 << 3), /* 0b0000000000001000 */
      kCGDisplayAddFlag                 = (1 << 4), /* 0b0000000000010000 */
      kCGDisplayRemoveFlag              = (1 << 5), /* 0b0000000000100000 */
      kCGDisplayEnabledFlag             = (1 << 8), /* 0b0000000100000000 */
      kCGDisplayDisabledFlag            = (1 << 9), /* 0b0000001000000000 */
      kCGDisplayMirrorFlag              = (1 << 10),/* 0b0000010000000000 */
      kCGDisplayUnMirrorFlag            = (1 << 11),/* 0b0000100000000000 */
      kCGDisplayDesktopShapeChangedFlag = (1 << 12) /* 0b0001000000000000 */
    };
    

    Now you can add them (or "or" them) and get different values

    kCGDisplayAddFlag | kCGDisplayDisabledFlag /* 0b0000001000010000 */
    
    0 讨论(0)
  • 2020-12-02 11:09

    If you have FlagA=1, FlagB=2 and FlagC=3, FlagA or FlagB would give the same value as FlagC. The shift operator is used to ensure that every combination of flags is unique.

    0 讨论(0)
  • 2020-12-02 11:11

    This will allow for a variable to easily combine multiple flags:

    unit32_t multFlag = kCGDisplayRemoveFlag | kCGDisplayMirrorFlag | kCGDisplaySetMainFlag'
    
    0 讨论(0)
  • 2020-12-02 11:14

    Let's me give you a more practice example. In c++ when you want to open a file (Open for output, and in binary mode opposed to text mode), you can do it by:

    const char *filename = "/home/xy/test.bin";
    fstream output(filename, ios::out | ios::binary);
    

    You can see, ios::out | ios::binary can set two mode(Open for output, and in binary mode).

    How does this work ? It's by enum(bitwise-shift values):

    enum _Ios_Openmode 
    { 
      _S_app        = 1L << 0,
      _S_ate        = 1L << 1,
      _S_bin        = 1L << 2,  /* 0b0000000000000100 */
      _S_in         = 1L << 3,
      _S_out        = 1L << 4,  /* 0b0000000000010000 */
      _S_trunc      = 1L << 5
      //.....
    };
    
    /// Perform input and output in binary mode (as opposed to text mode).
    static const openmode binary =  _S_bin;
    
    /// Open for input.  Default for @c ifstream and fstream.
    static const openmode in =      _S_in;
    
    /// Open for output.  Default for @c ofstream and fstream.
    static const openmode out =     _S_out;
    

    If you use value increment by 1 in enum _Ios_Openmode, you have to set(ios::out) and set(ios::binary) do two times. It may not so convenient to check and set value by one time.

    0 讨论(0)
提交回复
热议问题