visual-c++enumsflags

What is the maximum enum flag value I can use here?


Sorry if this has been asked before. I have always struggled with the concept of flags, even though I use them on occasion.

Take this enum:

enum ImportAssignment
{
    None              = 0,
    OCLMHost          = 1 << 0,
    OCLMCohost        = 1 << 1,
    OCLMZoomAttendant = 1 << 2,
    OCLMChairman      = 1 << 3,
    OCLMOpenPrayer    = 1 << 4,
    OCLMClosePrayer   = 1 << 5,
    OCLMConductorCBS  = 1 << 6,
    OCLMReaderCBS     = 1 << 7,
    PTHost            = 1 << 8,
    PTCohost          = 1 << 9,
    PTZoomAttendant   = 1 << 10,
    PTChairman        = 1 << 11,
    PTHospitality     = 1 << 12,
    WTConductor       = 1 << 13,
    WTReader          = 1 << 14,
    PTSpeaker         = 1 << 15,
    PTTheme           = 1 << 16
};

What would be the largest value I can use here? As in 1 << nn? What maximum value and nn be and why is it that value?


The suggested duplicate:

What is the underlying type of a c++ enum?

Appears to only explain that the underlying variable type of an enum is an int. I already know this. But I still don't really know how large the nn value can be and I do not see how the linked question addresses that.


Solution

  • The logical bitwise shift to the left moves 1 in your example one bit to the left.

    | Operation | Binary | Decimal |
    |:--------- |:------:| -------:|
    | 1<<0      | 00001  | 1       |
    | 1<<1      | 00010  | 2       |
    | 1<<2      | 00100  | 4       |
    | 1<<3      | 01000  | 8       |
    | 1<<4      | 10000  | 16      |
    

    Therefore, the biggest nn number would be 63 for the int64 on your platform, 31 if the int is int32.