| 253 | } |
| 254 | |
| 255 | [[nodiscard]] Status SetBitfield(uint32_t bit_offset, uint32_t bits, uint64_t value) { |
| 256 | uint32_t first_byte = bit_offset / 8; |
| 257 | uint32_t last_byte = (bit_offset + bits - 1) / 8 + 1; |
| 258 | Status bound_status = checkLegalBound(first_byte, last_byte - first_byte); |
| 259 | if (!bound_status) { |
| 260 | return bound_status; |
| 261 | } |
| 262 | |
| 263 | bit_offset -= byte_offset_ * 8; |
| 264 | while (bits--) { |
| 265 | bool v = (value & (uint64_t(1) << bits)) != 0; |
| 266 | uint32_t byte = bit_offset >> 3; |
| 267 | uint32_t bit = 7 - (bit_offset & 7); |
| 268 | uint32_t byteval = buf_[byte]; |
| 269 | byteval &= ~(1 << bit); |
| 270 | byteval |= int(v) << bit; |
| 271 | buf_[byte] = char(byteval); |
| 272 | bit_offset++; |
| 273 | } |
| 274 | return Status::OK(); |
| 275 | } |
| 276 | |
| 277 | private: |
| 278 | // The bit field cannot exceed 64 bits, takes an extra byte when it unaligned. |
no outgoing calls
no test coverage detected