David Rath David Rath - 1 year ago 98
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;


bool output = true&&true;

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

Answer Source

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();


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 &.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download