▲ 283 ▼ Infallible Code (feddit.uk) submitted 1 year ago by cm0002@lemmy.world to c/programmer_humor@programming.dev 57 comments fedilink hide all child comments
[–] lambda@programming.dev 14 points 1 year ago (1 child) Now see, you need the other method. IsNegativeEven() permalink fedilink source parent hideshow 2 child comments replies: [–] sus@programming.dev 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(); } permalink fedilink source parent hideshow 2 child comments replies: [–] UnknownSoul@programming.dev 2 points 1 year ago Nice, now we just need another function to find odd numbers, cause not all numbers are even you know. permalink fedilink source parent
[–] sus@programming.dev 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(); } permalink fedilink source parent hideshow 2 child comments replies: [–] UnknownSoul@programming.dev 2 points 1 year ago Nice, now we just need another function to find odd numbers, cause not all numbers are even you know. permalink fedilink source parent
[–] UnknownSoul@programming.dev 2 points 1 year ago Nice, now we just need another function to find odd numbers, cause not all numbers are even you know. permalink fedilink source parent