diff options
Diffstat (limited to 'chilbert/Operations.hpp')
-rw-r--r-- | chilbert/Operations.hpp | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/chilbert/Operations.hpp b/chilbert/Operations.hpp index e4a23b5..c50136a 100644 --- a/chilbert/Operations.hpp +++ b/chilbert/Operations.hpp @@ -22,6 +22,7 @@ #include <cassert> #include <climits> #include <cstddef> +#include <cstdint> #include <type_traits> namespace chilbert { @@ -87,6 +88,20 @@ ffs<unsigned long long>(const unsigned long long field) return __builtin_ffsll(field); } +/// Calculates the inverse Gray Code of `value` in place +template <typename T> +std::enable_if_t<!std::is_integral<T>::value> grayCodeInv(T& value); + +/// Implementation of grayCodeInv for any integral type +template <typename T> +std::enable_if_t<std::is_integral<T>::value> +grayCodeInv(T& value) +{ + for (T shift = 1; shift < sizeof(T) * CHAR_BIT; shift <<= 1) { + value ^= (value >> shift); + } +} + } // namespace chilbert #endif |