[using % to obtain integer remainder after division] is the way that
I have always [tested for even/odd], although it occurs to me
that AND-ing a value with one may be a significantly simpler computation
than calculating the remainder of a divide by two in the majority of
circumstances. This method might be worth considering for unsigned
integers.
This is true; but at the same time, on any machine where it matters,
any optimizing compiler worthy of the word "optimizing" should turn:
x % constant
into:
x & (constant - 1)
whenever the given constant is a power of two, because these always
produce the same result (for an unsigned x).
For signed integers (and still power-of-two constants), the process
is a bit more difficult -- a typical two's complement signed integer
gives different answers for "x % CONST" and "x & (CONST-1)" when
x is negative. There are bit-twiddling tricks that can be used if
the value is required, though; and if only the "truth-ness" of the
value is of interest, the above transform again works. That is:
if (x % 8)
and:
if (x & 7)
are both true (or false) in the same sets of cases, even when x is
signed, as long as the machine uses two's complement. This allows
an optimizing compiler to "do the right thing".