Q:

I was asked this in a recent interview q

I was asked this in a recent interview:
you can use a number or math exprretion not boolean phrase:
int x = ???;

while(true)
{
    x=???;
    console.write(x + ",");
}
 Output: 9,2,9,2,9,2...

So this answer is not a good one:
 int x = 2;
 while(true)
 {
     x = x == 2 ? 9:2;
 }

Ideas anyone?

A:

Use the xor (^) operator:
int x = 2;

while(true)
{
    x = x ^ 11; // equivalent to x = x ^ 9 ^ 2
    Console.Write(x + ",");
}

Because
9 ^ 11 == 2

and
2 ^ 11 == 9

Another way, cheating heavily:
x = (x / 9) * 2 + 
    (x + unchecked((int)(uint.MaxValue - 8))) / (-7) * 9;

This is based on how int are built and what happens if there is an overflow.
(x / 9) * 2

if x == 2, then x / 9 == 0, so 0 * 2 == 0
if x == 9, then 9 / 9 == 1, so 1 * 2 == 2
(x + unchecked((int)(uint.MaxValue - 8))) / (-7) * 9

if x == 2 then x + unchecked((int)(uint.MaxValue - 8)) == -7, so -7 / (-7) * 9 == 9
if x == 9, then x + unchecked((int)(uint.MaxValue - 8)) == 0, so 0 / (-7) * 9 == 0
The two parts are summed together, so 0 + 9 or 2 + 0.
Or clearly
x = (x + unchecked((int)(uint.MaxValue - 1))) / 7 * 2 +
    (x + unchecked((int)(uint.MaxValue - 8))) / (-7) * 9;

where we reuse the "overflow" method twice.
In general there are probably many other solutions that are based on this principle: you build an equation that is 0 when x is 2 and is something else when x is 9, and another equation that is 0 when x is 9 and is something else when x is 2. Then you "normalize" the something else to 1 (like the / 7 and / -7 I used) and multiply it by 2 and by 9. Probably you can build something similar with SIN or COS.
(clearly I can't find easy solutions... five minutes later I comprehended that it was useless to use overflows... The minus operator uses the 2 complement of integers directly :-) )
x = (x - 2) / 7 * 2 +
    (x - 9) / (-7) * 9;

