- Home /
could someone explain this line "Color color = ((x & y) != 0 ? Color.white : Color.gray);"
I'm learning and I found this tutorial unity but not something I've ever seen xc
That is a Ternary operator, It goes like this; (Condition) ? (ActionIfTrue):(ActionIfFalse); Just a short hand for an if else statement really. In the example you posted, it's saying that if color x and y aren't 0, color = white, else color = gray.
Answer by getyour411 · Aug 12, 2015 at 12:49 AM
Look into the ternary operator
There's also a Bitwise Operation being utilized in there.
Namely, what it's saying is that if both integers "x" and "y" share ANY power of two in common, then the color will be assigned as white. If the numbers are both zero or none of the bits are matching, then the color will be grey ins$$anonymous$$d.
// Bitwise numeric alignment for AND operator (&)
IN:
00000100 -- 4
00000001 -- 1
||||||||
vvvvvvvv RESULT OF (4 & 1):
00000000 -- 0
IN:
00001011 -- 11
00000110 -- 6
||||||||
vvvvvvvv RESULT OF (11 & 6):
00000010 -- 2
In the first case, you see that 4 and 1 share no binary 1's when compared against one another vertically.
In the second, however, 11 and 6 in binary DO share a common value, but only one. Because there was a match, the result would not be equal to zero and, therefore, would fit the condition of ((x & y) != 0).
Edit: Added arrows to code example for clarity
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Smooth Camera Background Color Changing? 1 Answer
Distribute terrain in zones 3 Answers
Is it possible to give a color to int variables? 1 Answer
How to change a buttons color? 2 Answers