Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Left Shift Operator

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 working on a game in Unity).

private readonly int FRUIT_LAYERS =
          (1 << LayerMask.NameToLayer("Apple"))
        | (1 << LayerMask.NameToLayer("Banana"));

NameToLayer takes a string and returns an integer. I've always seen left shift operators used with the constant integer on the right side, not the left, and all the examples I'm finding via Google follow that approach. In this case, I think he's pushing Apple and Banana onto the same relative layer (which I'll use later for filtering). In the future there would be more "fruits" to filter by. Any brilliant stackoverflowers who can give me an explanation of what's happening on those lines?

like image 376
David Avatar asked Dec 08 '22 16:12

David


2 Answers

1 << x is essentially saying "give me a number where the (x+1)-th bit is one and the rest of the numbers are all zero.

x | y is a bitwise OR, so it will go through each bit from 1 to n and if that bit is one in either x or y then that bit will be one in the result, if not it will be zero.

So if LayerMask.NameToLayer("Apple") returns 2 and LayerMask.NameToLayer("Banana") returns 3 then FRUIT_LAYERS will be a number with the 3rd and 4th bits set, which is 1100 in binary, or 12 in base 10.

like image 132
Servy Avatar answered Dec 11 '22 04:12

Servy


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;
like image 34
Timothy Shields Avatar answered Dec 11 '22 04:12

Timothy Shields