From 55f7d54ea98fcec0eb7e224e5d54e63c2a28de49 Mon Sep 17 00:00:00 2001 From: polymurph Date: Mon, 23 Aug 2021 08:51:58 +0200 Subject: [PATCH] added BitField to utils --- utils/BitFiled.hpp | 76 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 utils/BitFiled.hpp diff --git a/utils/BitFiled.hpp b/utils/BitFiled.hpp new file mode 100644 index 0000000..c3f4a3a --- /dev/null +++ b/utils/BitFiled.hpp @@ -0,0 +1,76 @@ +/** + * @file BitField.h + * @author Edwin Koch (eddyed.k@gmail.com) + * @brief + * @version 0.1 + * @date 2020-12-19 + * + * @copyright Copyright (c) 2020 + * + */ + +// Based on: +// https://www.youtube.com/watch?v=TYqbgvHfxjM +// https://stackoverflow.com/questions/31726191/is-there-a-portable-alternative-to-c-bitfields +// https://stackoverflow.com/questions/1392059/algorithm-to-generate-bit-mask +#ifndef _BITFIELDS_HPP_ +#define _BITFIELDS_HPP_ +#include +/** + * @brief Template class for portable Bitfields + * + * @tparam T type of variable in which the bitfield resides + * @tparam START bit index starting from LSB where the bitfield starts + * @tparam SIZE number of bits + */ +template +struct BitField +{ + /** + * @brief Construct a new Bit Field object + * + */ + BitField() + { + static_assert(SIZE != 0, "Bitfield SIZE must be > 0!"); + static_assert(START < sizeof(T) * 8, "START exceeds number of bits of the chosen typename T!"); + } + + /** + * @brief assignment operator + * + * @param v value to be written in the bitfield + * @return BitField& + */ + BitField& operator =(T v) + { + // use bit band alias if system permits + _raw = ((v & ((1ULL << SIZE)-1)) << START) | (_raw & ~(((1ULL << SIZE)-1) << START)); + return *this; + } + + /** + * @brief return the value inside the bitfield + * + * @return T + */ + operator T() const + { + return (_raw >> START) & ((1ULL << SIZE) - 1); + } + + /** + * @brief return the raw value + * + * @return T + */ + T operator ()() const + { + return (_raw >> START) & ((1ULL << SIZE) - 1); + } + + private: + T _raw; +}; + +#endif // _BITFIELDS_HPP_