bitwise: offset should start from least significant bits

This commit is contained in:
Danny Robson 2019-02-03 13:52:56 +11:00
parent ddc24b076b
commit cf268a1960

View File

@ -140,7 +140,7 @@ namespace cruft {
/// ///
/// \tparam ValueT the underlying integral type /// \tparam ValueT the underlying integral type
/// \tparam OffsetV the index of the first bit of the range (where 0 is /// \tparam OffsetV the index of the first bit of the range (where 0 is
/// most significant) /// least significant)
/// \tparam SizeV the number of bits in the range. Must be strictly /// \tparam SizeV the number of bits in the range. Must be strictly
/// positive. /// positive.
template < template <
@ -148,11 +148,10 @@ namespace cruft {
size_t OffsetV, size_t OffsetV,
size_t SizeV size_t SizeV
> >
class bitfield { class [[gnu::packed]] bitfield {
public: public:
static_assert (SizeV > 0); static_assert (SizeV > 0);
static_assert (OffsetV + SizeV <= sizeof (ValueT) * 8); static_assert (OffsetV + SizeV <= sizeof (ValueT) * 8);
static_assert (sizeof (ValueT) == sizeof (bitfield));
decltype(auto) decltype(auto)
operator+ () const { return +value; } operator+ () const { return +value; }
@ -160,9 +159,7 @@ namespace cruft {
operator auto() const operator auto() const
{ {
auto const MASK = (1u << SizeV) - 1u; auto const MASK = (1u << SizeV) - 1u;
auto const SHIFT = sizeof (ValueT) * 8 - OffsetV - SizeV; return (value >> OffsetV) & MASK;
return (value >> SHIFT) & MASK;
} }
ValueT value; ValueT value;