semf
bitsetutility.h
Go to the documentation of this file.
1
10#ifndef SEMF_UTILS_PROCESSING_BITSETUTILITY_H_
11#define SEMF_UTILS_PROCESSING_BITSETUTILITY_H_
12
13#include <cstdint>
14#include <bitset>
15
16namespace semf
17{
23{
24public:
31 template <size_t N>
32 static bool parityIsOdd(std::bitset<N> bits);
33
42 template <size_t N>
43 static std::bitset<N> createBitset(const uint8_t data[], size_t dataSize, size_t firstBit);
44
54 template <size_t N>
55 static size_t insertBitsetInBuffer(std::bitset<N> bitset, uint8_t buffer[], size_t bufferSize, size_t firstBit);
56};
57
58template <size_t N>
59bool BitsetUtility::parityIsOdd(std::bitset<N> bits)
60{
61 return bits.count() % 2;
62}
63
64template <size_t N>
65std::bitset<N> BitsetUtility::createBitset(const uint8_t data[], size_t dataSize, size_t firstBit)
66{
67 std::bitset<N> bitset;
68
69 for (size_t i = 0; (i < N) && (((firstBit + i) / 8) < dataSize); i++)
70 {
71 bitset.set(i, data[(firstBit + i) / 8] & (1 << ((firstBit + i) % 8)));
72 }
73
74 return bitset;
75}
76
77template <size_t N>
78static size_t insertBitsetInBuffer(std::bitset<N> bitset, uint8_t buffer[], size_t bufferSize, size_t firstBit)
79{
80 size_t i = 0;
81
82 for (; (i < N) && (((firstBit + i) / 8) < bufferSize); i++)
83 {
84 if (bitset[i])
85 buffer[(firstBit + i) / 8] |= static_cast<uint8_t>((1 << ((firstBit + i) % 8)));
86
87 else
88 buffer[(firstBit + i) / 8] &= static_cast<uint8_t>(~(1 << ((firstBit + i) % 8)));
89 }
90
91 return i;
92}
93} /* namespace semf */
94#endif // SEMF_UTILS_PROCESSING_BITSETUTILITY_H_
Useful helper functions for handeling bitsets.
Definition: bitsetutility.h:23
static std::bitset< N > createBitset(const uint8_t data[], size_t dataSize, size_t firstBit)
Copies a part of an array into a bitset.
Definition: bitsetutility.h:65
static size_t insertBitsetInBuffer(std::bitset< N > bitset, uint8_t buffer[], size_t bufferSize, size_t firstBit)
Insert a bitset in a array.
static bool parityIsOdd(std::bitset< N > bits)
Returns the parity of bits in a bitset.
Definition: bitsetutility.h:59
static size_t insertBitsetInBuffer(std::bitset< N > bitset, uint8_t buffer[], size_t bufferSize, size_t firstBit)
Definition: bitsetutility.h:78