Context:
I'm learning C# and have been messing about on the Pex for fun site. The site challenges you to re-implement a secret algorithm, by typing code into the site and examining how the inputs and outputs differ between your implementation and the secret implementation.
Problem:
Anyway, I got stuck on a basic code duel called XAndY.
From the name it seemed obvious that the answer was just:
public static bool Puzzle(bool x, bool y)
{
return x && y;
}
However, this was incorrect and Pex informed me that the following inputs produced a different result than the secret implementation:
Input:
x:true y:true (0x02)
Output:
my implementation: true (0x02)
secret implementation: false
Mismatch Your puzzle method produced the wrong result.
Code: Puzzle(true, PexSafeHelpers.ByteToBoolean((byte)2));
After a lot of confusion trying to compare different types of true, I realised that the implementation that Pex was looking for was actually just using a bitwise AND:
return x & y;
Questions:
I thought that for both semantic and short-circuiting reasons you should use logical &&
for comparing boolean values, but regardless:
- Does this mean that
x & y
andx && y
definitively do not have the same outputs for all possible bool arguments? (or could it be something buggy in Pex?) - Does this mean you can differentiate between different values of bool
true
in C#? If so, how?
true
andtrue
it should outputfalse
.&
doesn't do that, so it would seem that that is not the actual implementation. – BouldinSystem.Convert
class. – Corrugatetrue
orfalse
. – Corrugate&
is a perfectly legal boolean operator in C#. The CLR implementation is a little suspect but practical. – Corrugatex ^ y
– Inlet