return 0;
}
- union {
- uint64_t whole;
- uint8_t bytes[sizeof(uint64_t)];
- } combined;
- copy_bits_right_aligned(source, source_length, offset, bit_count,
- combined.bytes, sizeof(combined.bytes));
- if(BYTE_ORDER == LITTLE_ENDIAN) {
- combined.whole = __builtin_bswap64(combined.whole);
+ ArrayOrBytes combined;
+ memset(combined.bytes, 0, sizeof(combined.bytes));
+ if(copy_bits_right_aligned(source, source_length, offset, bit_count,
+ combined.bytes, sizeof(combined.bytes))) {
+ if(BYTE_ORDER == LITTLE_ENDIAN) {
+ combined.whole = __builtin_bswap64(combined.whole);
+ }
+ } else {
+ // debug("couldn't copy enough bits from source")
}
return combined.whole;
}
destination_length, nibble_index * NIBBLE_SIZE);
}
+bool set_bitfield(const uint64_t value, const uint16_t offset,
+ const uint16_t bit_count, uint8_t destination[],
+ uint16_t destination_length) {
+ if(value > bitmask(bit_count)) {
+ return false;
+ }
+
+ ArrayOrBytes combined = {
+ whole: value
+ };
+
+ if(BYTE_ORDER == LITTLE_ENDIAN) {
+ combined.whole = __builtin_bswap64(combined.whole);
+ }
+
+ return copy_bits(combined.bytes, sizeof(combined.bytes),
+ sizeof(combined.bytes) * CHAR_BIT - bit_count, bit_count,
+ destination, destination_length, offset);
+}