- Home /
What is the difference between these ?
hey all,
i just wanted to know what is the difference between these | and ||
i know the double lines are Or what is the single ?
Answer by Owen-Reynolds · May 15, 2011 at 05:48 AM
A single | or & is a bitwise operator, for binary-type math. For example 6 in binary is 110 and 12 in binary is 1100. 6|12 means to use all the bits in 6 OR in 12, which is 1110, or 14.
Layers are stored as binary, putting a 1 in the slot for that layer. Layer 0 is 00000001, layer 1 is 00000010 layer 2 is 00000100, and so on. This is why there are layers 0-31. Computer numbers are 32-bits long. A layer is always 31 zeros and a 1 in the slot for that layer.)
If layers were stored as normal numbers, and you wanted to skip 2, 3 and 6 for a raycast, you'd have to write if(layer!=2 && layer!=3 && layer!=6)
. Instead, you write the layers you want to skip using the binary layer representation:
76543210
A 00001000 <-- I am on layer 3 B 01001100 <-- skip layers are 6, 3 and 2 --------
A&B 00001000 <-- a 1 in slots where both are 1's A|B 01001100 <-- a 1 in slots where either are 1's
Using (layer&skipLayers)
very quickly checks whether the current layer is 2, 3 or 6 (a result of not 0 means you are in the skipLayers.)
If you using using 01001100 to represent skipping 6,3 and 2, and want to add layer 5, you can write: skipLayers=skipLayers|(1<<5);
to get 01101100. The double-lessThan symbol is a bitshift. If means to turn 1 into 100000 by shifting it five spaces over.
Your answer
Follow this Question
Related Questions
how do i use 'And' and 'Or' in UnityScript 2 Answers
Where did I go wrong (logical operator or tagging) 1 Answer
Bitwise Operators 3 Answers
Bitwise & Operator in Shaders? 1 Answer
Unity/Javascript Operators 2 Answers