1 #include "otsdaq/BitManipulator/BitManipulator.h"
5 #include "otsdaq/Macros/CoutMacros.h"
6 #include "otsdaq/MessageFacility/MessageFacility.h"
11 BitManipulator::BitManipulator() {}
14 BitManipulator::~BitManipulator() {}
17 uint64_t BitManipulator::insertBits(uint64_t& data,
19 unsigned int startBit,
20 unsigned int numberOfBits)
24 for(
unsigned int i = 0; i < numberOfBits; i++)
25 data &= ~((uint64_t)1 << (startBit + i));
27 data |= (value << startBit);
34 uint64_t BitManipulator::insertBits(std::string& data,
36 unsigned int startBit,
37 unsigned int numberOfBits)
40 const unsigned int bitsInAByte = 8;
42 uint8_t overWritten = 0;
43 int startByte = startBit / bitsInAByte;
44 int finalByte = (startBit + numberOfBits - 1) / bitsInAByte;
45 int startBitInByte = startBit % bitsInAByte;
46 int finalBitInByte = (startBit + numberOfBits - 1) % bitsInAByte;
49 int lastByteLength = (startBit + numberOfBits) % bitsInAByte;
55 for(
int j = 0; j <= finalByte - startByte; ++j)
59 if(j != finalByte - startByte)
62 finalBitInByte = (startBit + numberOfBits - 1) % 8;
65 finalBitInByte = 7 - startBitInByte;
66 startBitInByte = 7 - tmp;
72 overWritten = data.substr(startByte + j, 1).data()[0];
78 for(
int y = 0; y <= finalBitInByte - startBitInByte; ++y)
80 if(finalByte - startByte > 1)
82 if(j != finalByte - startByte)
83 toWrite |= ((value >> (lastByteLength +
84 (finalByte - startByte - 1 - j) * 8 + y)) &
88 toWrite |= ((value >> (lastByteLength + y)) & 1) << y;
90 else if(finalByte - startByte == 1)
91 toWrite |= ((value >> (lastByteLength * (1 - j) + y)) & 1) << y;
92 else if(finalByte - startByte == 0)
93 toWrite |= ((value >> y) & 1) << y;
101 for(
int n = 0; n < finalBitInByte - startBitInByte + 1; n++)
103 overWritten &= ~((uint64_t)1 << (startBitInByte + n));
108 overWritten |= (toWrite << startBitInByte);
112 data[startByte + j] = overWritten;
118 return (uint64_t)overWritten;
122 uint64_t BitManipulator::reverseBits(uint64_t data,
123 unsigned int startBit,
124 unsigned int numberOfBits)
126 uint64_t reversedData = 0;
127 for(
unsigned int r = startBit; r < numberOfBits; r++)
128 reversedData |= ((data >> r) & 1) << (numberOfBits - 1 - r);
133 uint32_t BitManipulator::insertBits(uint32_t& data,
135 unsigned int startBit,
136 unsigned int numberOfBits)
140 value = value << startBit;
141 for(
unsigned int i = 0; i < 32; i++)
143 if(i >= startBit && i < startBit + numberOfBits)
144 data &= ~((uint32_t)1 << i);
146 value &= ~((uint32_t)1 << i);
155 uint32_t BitManipulator::insertBits(std::string& data,
157 unsigned int startBit,
158 unsigned int numberOfBits)
161 const unsigned int bitsInAByte = 8;
163 uint8_t overWritten = 0;
164 int startByte = startBit / bitsInAByte;
165 int finalByte = (startBit + numberOfBits - 1) / bitsInAByte;
166 int startBitInByte = startBit % 8;
167 int finalBitInByte = (startBit + numberOfBits - 1) % bitsInAByte;
170 int lastByteLength = (startBit + numberOfBits) % bitsInAByte;
176 for(
int j = 0; j <= finalByte - startByte; ++j)
180 if(j != finalByte - startByte)
183 finalBitInByte = (startBit + numberOfBits - 1) % 8;
185 tmp = finalBitInByte;
186 finalBitInByte = 7 - startBitInByte;
187 startBitInByte = 7 - tmp;
193 overWritten = data.substr(startByte + j, 1).data()[0];
198 toWrite = (uint8_t)0;
199 for(
int y = 0; y <= finalBitInByte - startBitInByte; ++y)
201 if(finalByte - startByte > 1)
203 if(j != finalByte - startByte)
204 toWrite |= ((value >> (lastByteLength +
205 (finalByte - startByte - 1 - j) * 8 + y)) &
209 toWrite |= ((value >> (lastByteLength + y)) & 1) << y;
211 else if(finalByte - startByte == 1)
212 toWrite |= ((value >> (lastByteLength * (1 - j) + y)) & 1) << y;
213 else if(finalByte - startByte == 0)
214 toWrite |= ((value >> y) & 1) << y;
222 for(
int n = 0; n < finalBitInByte - startBitInByte + 1; n++)
224 overWritten &= ~((uint32_t)1 << (startBitInByte + n));
229 overWritten |= (toWrite << startBitInByte);
233 data[startByte + j] = overWritten;
239 return (uint32_t)overWritten;
243 uint32_t BitManipulator::reverseBits(uint32_t data,
244 unsigned int startBit,
245 unsigned int numberOfBits)
247 uint32_t reversedData = 0;
248 for(
unsigned int r = startBit; r < startBit + numberOfBits; r++)
249 reversedData |= ((data >> r) & 1) << (numberOfBits - 1 - r);
254 uint32_t BitManipulator::readBits(uint32_t data,
255 unsigned int startBit,
256 unsigned int numberOfBits)
258 uint32_t returnData = 0;
259 for(
unsigned int r = startBit; r < startBit + numberOfBits; r++)
260 returnData += ((data >> r) & 0x1) << (r - startBit);