This C++ solution supports a number of any size (byte, 16-bit, 32-bit, etc), and compiles down to just a single CPU instruction for every invocation of any_odd.
constexprunsignedlonglongodd_bits{0b1010101010101010101010101010101010101010101010101010101010101010ull};template<typenameT>inlineboolany_odd(constT&test){static_assert(std::is_integral<T>::value,"any_odd only works with integers");return(*reinterpret_cast<constT*>(&odd_bits)&test)!=0;}
Note that without the reinterpret_cast, an unnecessary cdqe instruction will likely be introduced, even with maximum optimization turned on.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
This C++ solution supports a number of any size (byte, 16-bit, 32-bit, etc), and compiles down to just a single CPU instruction for every invocation of
any_odd
.Note that without the
reinterpret_cast
, an unnecessarycdqe
instruction will likely be introduced, even with maximum optimization turned on.