Simon,

The (intended) comparison is actually just done once.  The surprising fact
is that MSIL doesn't have a "cnq" (compare not equal) instruction, only a
"ceq" (compare equal) instruction.

So if you compare a test value for inequality to zero, the MSIL code must
first determine the test value's equality to zero (because that's all it
can do), then invert the result by comparing it once again to zero (=
boolean false) to get the desired result (= boolean true if the test value
was not zero).

I would guess that the JIT compiler will recognize this sequence and
subsitute a machine language instruction that directly compares for
inequality to zero.

Cheers, Chris


At 02:01 27.02.2003 -0800, you wrote:
Given this source code

C# source:
 public bool IsIndexed {
  get { return (SearchFlags & (int)
AttributeSearchFlags.Index) != 0; }
 }


why is the comparison to 0 done twice?




Anakrino decompilation:
public bool get_IsIndexed() {
 return this.SearchFlags & 1 == 0 == 0;
}


ILDASM decompilation: { // Code size 15 (0xf) .maxstack 2 IL_0000: ldarg.0 IL_0001: call instance int32 SimmoTech.ActiveDirectory.ADAttributeSchema::get_SearchFlags() IL_0006: ldc.i4.1 IL_0007: and IL_0008: ldc.i4.0 IL_0009: ceq IL_000b: ldc.i4.0 IL_000c: ceq IL_000e: ret } // end of method ADAttributeSchema::get_IsIndexed

Reply via email to