• λλλ
    link
    fedilink
    1311 days ago

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

    • @[email protected]
      link
      fedilink
      710 days ago

      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();
      }
      
      • @[email protected]
        link
        fedilink
        110 days ago

        Nice, now we just need another function to find odd numbers, cause not all numbers are even you know.