One typical way of calculating this is to use x % 2. This involves a division operation, which is intrinsically slow. A better way to calculate this is to use x & 1. Even this is slower than it needs to be. For a multiple precision integer, only the least significant digit needs to be used.
The default implementation of this method uses x & 1. If the type T does not implement operator&, then this method will have to be specialized for T.
Name | Type | Description |
---|---|---|
x | T | Some number. |
#include <nttl/nttlTraits.h> ... nttlTraits<int> tr; ... int x = 123; if( tr.IsOdd( x ) ) ... |