diff options
Diffstat (limited to 'src/bitfield/bitfield.c')
-rw-r--r-- | src/bitfield/bitfield.c | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/src/bitfield/bitfield.c b/src/bitfield/bitfield.c index 3b0c5fdd..52f368f5 100644 --- a/src/bitfield/bitfield.c +++ b/src/bitfield/bitfield.c @@ -2,6 +2,7 @@ #include <limits.h> #include <string.h> #include <stddef.h> +#include <endian.h> uint64_t bitmask(const uint8_t bit_count) { return (((uint64_t)0x1) << bit_count) - 1; @@ -26,8 +27,25 @@ uint8_t get_byte(const uint8_t source[], const uint8_t source_length, return 0; } +uint64_t get_bitfield(const uint8_t source[], const uint8_t source_length, + const uint16_t offset, const uint16_t bit_count) { + if(bit_count > 64 || bit_count < 1) { + // TODO error reporting? + 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)); + return htobe64(combined.whole); +} + bool set_nibble(const uint16_t nibble_index, const uint8_t value, uint8_t* destination, const uint16_t destination_length) { return copy_bits(&value, CHAR_BIT, NIBBLE_SIZE, NIBBLE_SIZE, destination, destination_length, nibble_index * NIBBLE_SIZE); } + |