Combining Enum Value using Bitmask

前端 未结 4 614
离开以前
离开以前 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 03:12

    You can get this effect by specifying all fields in the enum and increased in powers of two to get the bitmask effect. For example, in your case:

    enum State
    {
        minimizing = 1,
        maximizing = 2,
    
        minimized = 4,
        maximized = 8
    };
    

    So you can then have your combinations of (State.maximized | State.minimizing). However this won't apply the restriction of only being State.maximized or State.minimized. If you want to do that you could convert them to a single bit, but I imagine in this example you would want to be able to have a case where it is neither maximized nor minimized.

提交回复
热议问题