C# Left Shift Operator

前端 未结 4 543
梦如初夏
梦如初夏 2021-01-14 01:47

There\'s a statement a co-worker of mine wrote which I don\'t completely understand. Unfortunately he\'s not available right now, so here it is (with modified names, we\'re

4条回答
  •  臣服心动
    2021-01-14 02:32

    Your coworker is essentially using an int in place of a bool[32] to try to save on space. The block of code you show is analogous to

    bool[] FRUIT_LAYERS = new bool[32];
    FRUIT_LAYERS[LayerMask.NameToLayer("Apple")] = true;
    FRUIT_LAYERS[LayerMask.NameToLayer("Banana")] = true;
    

    You might want to consider a pattern more like this:

    [Flags]
    enum FruitLayers : int
    {
        Apple = 1 << 0,
        Banana = 1 << 1,
        Kiwi = 1 << 2,
        ...
    }
    
    private readonly FruitLayers FRUIT_LAYERS = FruitLayers.Apple | FruitLayers.Banana;
    

提交回复
热议问题