looked at the ref and came across some code I just didnt understand. Could anyone explain what happens at that line, like what does the questionmark and the “” sign do? what decides if “color” is white or gray?
function Start () {
// Create a new texture and assign it to the renderer’s material
var texture = new Texture2D(128, 128);
renderer.material.mainTexture = texture;
// Fill the texture with Sierpinski’s fractal pattern!
for (var y : int = 0; y < texture.height; ++y) {
for (var x : int = 0; x < texture.width; ++x) {
var color = (x&y) ? Color.white : Color.gray; // this???
texture.SetPixel (x, y, color);
}
}
// Apply all SetPixel calls
texture.Apply();
}
Not in this case. is functioning as a logical bitwise operator. The giveaway, aside from the fact that the algorithm wouldn’t make sense if it were a boolean operation, and that the operands are ints, is that it’s a single . is the superior choice for boolean operations, and the one you’ll see used everywhere for that purpose.
Sadly correct - apparently US is one of those languages where any integral value (other than 0) returns true.
If false false = false;
If true false = false;
If false true = false;
If true true = true;
I would point out that the boolean and the boolean have similar but ultimately different behaviors.
using UnityEngine;
using System.Collections;
public class Test : MonoBehaviour
{
// Use this for initialization
void Start()
{
if (false DoSomething(1)) ;
if (false DoSomething(2)) ;
}
bool DoSomething(int i)
{
print(i);
return true;
}
}
What’s hard to understand? That the operator which combines the two int’s in a a special way meaning more than just bottom left will be gray? For example 12 == 0?
Or that the C# has made a decision that one cannot implicitly convert int to bool, something that US hasn’t done, and I prefer the former approach?
I get what you’ve said, now. You were talking about what feeds the ?: operator when I thought you were talking about what feeds the operator. That is, I thought you were saying that x&y was a boolean operation, and at the same time, saying I was correct that it was a bitwise operation. Still, this I don’t get:
It seems to be some FUBAR due to the JS specification… I’m confused as to why they do it, but it is documented behavior according to google:
It first evaluates the left hand operand, and if this returns false then, without going any further, it returns false for the whole expression. Otherwise it returns the value of the second operand: true or false for a Boolean value, or the actual value itself if non-Boolean.