C# Left Shift Operator

前端 未结 4 579
梦如初夏
梦如初夏 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:29

    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.

提交回复
热议问题