From cf268a1960685e099a0de137c20a486afa9e1490 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Sun, 3 Feb 2019 13:52:56 +1100 Subject: [PATCH] bitwise: offset should start from least significant bits --- bitwise.hpp | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/bitwise.hpp b/bitwise.hpp index 596242c2..e6d9b3b0 100644 --- a/bitwise.hpp +++ b/bitwise.hpp @@ -140,7 +140,7 @@ namespace cruft { /// /// \tparam ValueT the underlying integral type /// \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 /// positive. template < @@ -148,11 +148,10 @@ namespace cruft { size_t OffsetV, size_t SizeV > - class bitfield { + class [[gnu::packed]] bitfield { public: static_assert (SizeV > 0); static_assert (OffsetV + SizeV <= sizeof (ValueT) * 8); - static_assert (sizeof (ValueT) == sizeof (bitfield)); decltype(auto) operator+ () const { return +value; } @@ -160,9 +159,7 @@ namespace cruft { operator auto() const { auto const MASK = (1u << SizeV) - 1u; - auto const SHIFT = sizeof (ValueT) * 8 - OffsetV - SizeV; - - return (value >> SHIFT) & MASK; + return (value >> OffsetV) & MASK; } ValueT value;