Combining Enum Value using Bitmask

前端 未结 4 634
离开以前
离开以前 2021-02-09 02:29

I understand it\'s possible to use bitmasks in enum values, but I don\'t know how to create it.

I have a simple enum :

enum State
{
    minimizing = 0,
          


        
4条回答
  •  遥遥无期
    2021-02-09 02:55

    Use a different bit for every value in your enumeration, such as:

    enum State 
    {
      minimizing = 0x01, // 00000001
      maximizing = 0x02, // 00000010
      minimized  = 0x04, // 00000100
      maximized  = 0x08  // 00001000
    }:
    

    Then, you can combine multiple values with bitwise or (minimizing | maximized) and test for values with bitwise and (bool is_minimized = (flags & minimized);).

提交回复
热议问题