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, uint64_t value,
unsigned int startBit,
unsigned int numberOfBits)
21 for(
unsigned int i = 0; i < numberOfBits; i++)
22 data &= ~((uint64_t)1 << (startBit + i));
24 data |= (value << startBit);
31 uint64_t BitManipulator::insertBits(std::string& data, uint64_t value,
unsigned int startBit,
unsigned int numberOfBits)
34 const unsigned int bitsInAByte = 8;
36 uint8_t overWritten = 0;
37 int startByte = startBit / bitsInAByte;
38 int finalByte = (startBit + numberOfBits - 1) / bitsInAByte;
39 int startBitInByte = startBit % bitsInAByte;
40 int finalBitInByte = (startBit + numberOfBits - 1) % bitsInAByte;
43 int lastByteLength = (startBit + numberOfBits) % bitsInAByte;
49 for(
int j = 0; j <= finalByte - startByte; ++j)
53 if(j != finalByte - startByte)
56 finalBitInByte = (startBit + numberOfBits - 1) % 8;
59 finalBitInByte = 7 - startBitInByte;
60 startBitInByte = 7 - tmp;
66 overWritten = data.substr(startByte + j, 1).data()[0];
72 for(
int y = 0; y <= finalBitInByte - startBitInByte; ++y)
74 if(finalByte - startByte > 1)
76 if(j != finalByte - startByte)
77 toWrite |= ((value >> (lastByteLength + (finalByte - startByte - 1 - j) * 8 + y)) & 1) << y;
79 toWrite |= ((value >> (lastByteLength + y)) & 1) << y;
81 else if(finalByte - startByte == 1)
82 toWrite |= ((value >> (lastByteLength * (1 - j) + y)) & 1) << y;
83 else if(finalByte - startByte == 0)
84 toWrite |= ((value >> y) & 1) << y;
92 for(
int n = 0; n < finalBitInByte - startBitInByte + 1; n++)
94 overWritten &= ~((uint64_t)1 << (startBitInByte + n));
99 overWritten |= (toWrite << startBitInByte);
103 data[startByte + j] = overWritten;
109 return (uint64_t)overWritten;
113 uint64_t BitManipulator::reverseBits(uint64_t data,
unsigned int startBit,
unsigned int numberOfBits)
115 uint64_t reversedData = 0;
116 for(
unsigned int r = startBit; r < numberOfBits; r++)
117 reversedData |= ((data >> r) & 1) << (numberOfBits - 1 - r);
122 uint32_t BitManipulator::insertBits(uint32_t& data, uint32_t value,
unsigned int startBit,
unsigned int numberOfBits)
126 value = value << startBit;
127 for(
unsigned int i = 0; i < 32; i++)
129 if(i >= startBit && i < startBit + numberOfBits)
130 data &= ~((uint32_t)1 << i);
132 value &= ~((uint32_t)1 << i);
141 uint32_t BitManipulator::insertBits(std::string& data, uint32_t value,
unsigned int startBit,
unsigned int numberOfBits)
144 const unsigned int bitsInAByte = 8;
146 uint8_t overWritten = 0;
147 int startByte = startBit / bitsInAByte;
148 int finalByte = (startBit + numberOfBits - 1) / bitsInAByte;
149 int startBitInByte = startBit % 8;
150 int finalBitInByte = (startBit + numberOfBits - 1) % bitsInAByte;
153 int lastByteLength = (startBit + numberOfBits) % bitsInAByte;
159 for(
int j = 0; j <= finalByte - startByte; ++j)
163 if(j != finalByte - startByte)
166 finalBitInByte = (startBit + numberOfBits - 1) % 8;
168 tmp = finalBitInByte;
169 finalBitInByte = 7 - startBitInByte;
170 startBitInByte = 7 - tmp;
176 overWritten = data.substr(startByte + j, 1).data()[0];
181 toWrite = (uint8_t)0;
182 for(
int y = 0; y <= finalBitInByte - startBitInByte; ++y)
184 if(finalByte - startByte > 1)
186 if(j != finalByte - startByte)
187 toWrite |= ((value >> (lastByteLength + (finalByte - startByte - 1 - j) * 8 + y)) & 1) << y;
189 toWrite |= ((value >> (lastByteLength + y)) & 1) << y;
191 else if(finalByte - startByte == 1)
192 toWrite |= ((value >> (lastByteLength * (1 - j) + y)) & 1) << y;
193 else if(finalByte - startByte == 0)
194 toWrite |= ((value >> y) & 1) << y;
202 for(
int n = 0; n < finalBitInByte - startBitInByte + 1; n++)
204 overWritten &= ~((uint32_t)1 << (startBitInByte + n));
209 overWritten |= (toWrite << startBitInByte);
213 data[startByte + j] = overWritten;
219 return (uint32_t)overWritten;
223 uint32_t BitManipulator::reverseBits(uint32_t data,
unsigned int startBit,
unsigned int numberOfBits)
225 uint32_t reversedData = 0;
226 for(
unsigned int r = startBit; r < startBit + numberOfBits; r++)
227 reversedData |= ((data >> r) & 1) << (numberOfBits - 1 - r);
232 uint32_t BitManipulator::readBits(uint32_t data,
unsigned int startBit,
unsigned int numberOfBits)
234 uint32_t returnData = 0;
235 for(
unsigned int r = startBit; r < startBit + numberOfBits; r++)
236 returnData += ((data >> r) & 0x1) << (r - startBit);