diff options
author | 2014-01-06 13:58:36 -0500 | |
---|---|---|
committer | 2014-01-06 13:58:36 -0500 | |
commit | eab0c42eac865a7e965878a7f2ad548371bd34d5 (patch) | |
tree | 1dc8c6e55cb51830bd0ca5de09d277d5ad206c84 /src/bitfield/bitfield.c | |
parent | cc2f44eeedac7d6e86005543c7e1596c3c78d551 (diff) |
Standardize order of arguments - destination is always last.
Diffstat (limited to 'src/bitfield/bitfield.c')
-rw-r--r-- | src/bitfield/bitfield.c | 24 |
1 files changed, 20 insertions, 4 deletions
diff --git a/src/bitfield/bitfield.c b/src/bitfield/bitfield.c index e40ab1ab..b244c9b1 100644 --- a/src/bitfield/bitfield.c +++ b/src/bitfield/bitfield.c @@ -34,10 +34,7 @@ uint64_t get_bitfield(const uint8_t source[], const uint8_t source_length, return 0; } - union { - uint64_t whole; - uint8_t bytes[sizeof(uint64_t)]; - } combined; + ArrayOrBytes combined; memset(combined.bytes, 0, sizeof(combined.bytes)); copy_bits_right_aligned(source, source_length, offset, bit_count, combined.bytes, sizeof(combined.bytes)); @@ -53,3 +50,22 @@ bool set_nibble(const uint16_t nibble_index, const uint8_t value, 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); +} |