0

Sorry if this has been asked before. I have always struggled with the concept of flags, even those 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.

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
  • Does this answer your question? [What is the underlying type of a c++ enum?](https://stackoverflow.com/questions/1122096/what-is-the-underlying-type-of-a-c-enum) – phuclv Oct 13 '22 at 13:16
  • @phuclv No. I already know that the `enum` is an `int`. 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. – Andrew Truckle Oct 13 '22 at 13:38
  • obviously it's `n - 1` if `int` is an n-bit number on your system. It's easy to check – phuclv Oct 14 '22 at 14:42

1 Answers1

1

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.

James Z
  • 12,209
  • 10
  • 24
  • 44