Without this “optimization”, those would yield an intuitive compile time error. As it stands, you’ll get unpredictable wrong answers at runtime if called on bad input.
In C++ at least, the proper replacement for this is either an overloaded set of functions taking different sized parameters (verbose), or a template with a concept restricting it to the correct set of types (complex). Otherwise you end up with conversions/promotions etc, or exactly the same problems as the macro (eg with an unconstrained template). If it's a pervasive utility function the latter is probably better, but if its a macro local to a source file doing a specific thing its not the end of the world
But it's just one of the obvious ways to check if a number is even? It's checking if the last digit is a 1 (odd) or 0 (not even). Checking the last digit is how I would check if an integer was even in real life too.
But checking the last digit is at least as simple as dividing by 2 and checking the remainder..?
I agree that this could've been a function, maybe with an always-inline attribute, but "C programmer uses macro for something which could have been a function" isn't exactly shittyprogramming; it's basically the norm. The macro has the advantage that it works with shorts, ints, longs, "uint8_t"s, unsigned long longs, "uint_fast16_t"s, ... Depending on its usage, it might have had to be replaced with a pretty big set of functions.
i'm under the assumption that if one is using bitwise-and in the first place, they know what they're doing and won't accidentally instantiate the macro with e.g. a float (probably a false assumption)
well if the programmer needs to copy code to implement something like isEven then the problems don't start and end with this thing being defined as a macro
this is also a shitpost sub so speculating on hypotheticals either way prob isn't too productive, in retrospect
62
u/zyxzevn Apr 16 '21 edited Apr 17 '21
It is missing a () around the n.
So when you call f_is_even(x+1) it becomes !(x + 1 & 1)
42
u/rafalb8 Apr 16 '21
Maybe it's stupid question, but why is it bad?
19
u/GRX13 Apr 16 '21
n & 1is equivalent ton % 2; this seems fine15
u/[deleted] Apr 16 '21
Everyone knows that shorter names makes faster programs.
57
u/HoldMyWater Apr 16 '21
What do you think this is? r/goodprogramming?