Historically (language B, predecessor C), bitwise operators (& |) were used as a logical operator, but since it does not perform a short circuit, they quickly correct the language and introduce short-circuited (& & ||)
In fact, because of this, the priority of bitwise operators still carries the same operator associativity so far. Until now, the bitwise operator (for example, its logical operator) is associated with comparisons, and not in an expression.
Case, this operator associativity ...
if (A + B == C)
... do not do it:
if (A & B == C)
In the second code, A is evaluated independently, and B == C are evaluated together. Therefore, we need to put brackets for bitwise operators to achieve the same operator associativity as mathematical operations.
if ( ( A & B ) == C )
Languages ββnot supported by C, Java and C # developers could break the tradition and give bitwise operators the same operator associativity as arithmetic operators. But they did not, so a large C-code base can be reused there in these languages.
Before that, I canβt understand why & and | do not behave the same as + - / * .
To answer your question, simply use the bitwise operator:
protected void Page_Load(object sender, EventArgs e) { bool isValid = A() & B(); Response.Output.Write("<br>Is Valid {0}", isValid); } bool A() { Response.Write("<br>TestA"); return false; } bool B() { Response.Write("<br>TestB"); return true; }
Michael buen
source share