2 Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
7 * @file CxxUtils/BitUnpacker.icc
8 * @author scott snyder <snyder@bnl.gov>
10 * @brief Helper to unpack a set of values bitwise from a stream.
14 #include "CxxUtils/ones.h"
22 * @param nbits Number of bits per item to use in the packed representation.
23 * @param stream Input stream object.
25 template <class STREAM>
27 BitUnpacker<STREAM>::BitUnpacker (uint8_t nbits, STREAM& stream)
32 m_mask (CxxUtils::ones<uint32_t>(nbits))
34 assert (m_nbits > 0 && m_nbits <= 32);
39 * @brief Unpack one value from the stream.
41 template <class STREAM>
43 uint32_t BitUnpacker<STREAM>::unpack()
45 const uint8_t totbits = 8*sizeof(m_buf);
52 uint32_t out = m_buf & m_mask;
53 if (m_nbits > m_nbuf) {
54 uint8_t nleft = m_nbits - m_nbuf;
56 out |= (m_buf & CxxUtils::ones<uint32_t>(nleft)) << m_nbuf;
58 m_nbuf = totbits - nleft;
74 * @param stream Input stream object.
76 template <class STREAM>
78 BitUnpacker8<STREAM>::BitUnpacker8 (STREAM& stream)
88 * @param nbits Must be 8.
89 * @param stream Input stream object.
91 template <class STREAM>
93 BitUnpacker8<STREAM>::BitUnpacker8 (uint8_t /*nbits*/, STREAM& stream)
102 * @brief Unpack one value from the stream.
104 template <class STREAM>
106 uint32_t BitUnpacker8<STREAM>::unpack()
113 uint8_t ret = m_buf & 0xff;
121 * @brief Constructor.
122 * @param stream Input stream object.
124 template <class STREAM>
126 BitUnpacker16<STREAM>::BitUnpacker16 (STREAM& stream)
135 * @brief Constructor.
136 * @param nbits Must be 16.
137 * @param stream Input stream object.
139 template <class STREAM>
141 BitUnpacker16<STREAM>::BitUnpacker16 (uint8_t /*nbits*/, STREAM& stream)
150 * @brief Unpack one value from the stream.
152 template <class STREAM>
154 uint32_t BitUnpacker16<STREAM>::unpack()
161 uint16_t ret = m_buf & 0xffff;
168 } // namespace CxxUtils