David Rath David Rath - 26 days ago 10
C# Question

What is more efficient, bitwise or equality checking?

I was just writing a few conditional variables, and figured it would be easier to set a value to the result of the equality between 2 boolean values.

What would be faster/more efficient from a processing side, obviously this is marginal?

bool output = true&true;


or

bool output = true&&true;


I expect the latter as its a single equality check rather than computing the entire byte

Answer

In your hypothetical example I doubt very much that it matters as evaluating true is crazy fast (if not compiled away completely)

Take this example however:

bool output = veryLongFunction() & otherVeryLongFunction();

vs

bool output = veryLongFunction() && otherVeryLongFunction();

In this case the double && short circuits the expression if the first evaluates to false and is thus faster than using just one &.

Comments