you are viewing a single comment's thread
view the rest of the comments
[–] 14 points 1 year ago (1 child)

Now see, you need the other method. IsNegativeEven()

  • source
  • parent
  • hideshow 2 child comments
  • [–] 9 points 1 year ago (1 child)

    We can avoid expensive branches (gasp) by using some bitwise arithmetic to achieve the so-called "absolute value", an advanced hacker technique I learnt at Blizzard. Also unlike c, c# is not enlightened enough to understand that my code is perfect so it complains about "not all code paths returning a value".

    private bool IsEven(int number)
    {
        number *= 1 - 2*(int)(((uint)number & 2147483648) >> 31);
        if (number > 1) return IsEven(number - 2);
        if (number == 0) return true;
        if (number == 1) return false;
        throw new Exception();
    }
    
  • source
  • parent
  • hideshow 2 child comments