filtering - C# Left Shift Operator -


there's statement co-worker of mine wrote don't understand. unfortunately he's not available right now, here (with modified names, we're working on game in unity).

private readonly int fruit_layers =           (1 << layermask.nametolayer("apple"))         | (1 << layermask.nametolayer("banana")); 

nametolayer takes string , returns integer. i've seen left shift operators used constant integer on right side, not left, , examples i'm finding via google follow approach. in case, think he's pushing apple , banana onto same relative layer (which i'll use later filtering). in future there more "fruits" filter by. brilliant stackoverflowers can give me explanation of what's happening on lines?

your coworker using int in place of bool[32] try save on space. block of code show analogous to

bool[] fruit_layers = new bool[32]; fruit_layers[layermask.nametolayer("apple")] = true; fruit_layers[layermask.nametolayer("banana")] = true; 

you might want consider pattern more this:

[flags] enum fruitlayers : int {     apple = 1 << 0,     banana = 1 << 1,     kiwi = 1 << 2,     ... }  private readonly fruitlayers fruit_layers = fruitlayers.apple | fruitlayers.banana; 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -