summaryrefslogtreecommitdiffstats
path: root/src/bitfield/8byte.c
diff options
context:
space:
mode:
authorChristopher Peplin <chris.peplin@rhubarbtech.com>2013-12-29 12:06:11 -0500
committerChristopher Peplin <chris.peplin@rhubarbtech.com>2013-12-29 12:06:11 -0500
commitf29f8a4cefbdc798c4a9aba495da7d2c0a81774c (patch)
treec3ec6e1cc8c75f30c4341812c3a7ad31445c51d9 /src/bitfield/8byte.c
parentc2c7ef1dfe4cae6b5831ffab0ce5780b27e0c25e (diff)
Check if a value will fit in bitfield before setting.
Diffstat (limited to 'src/bitfield/8byte.c')
-rw-r--r--src/bitfield/8byte.c11
1 files changed, 6 insertions, 5 deletions
diff --git a/src/bitfield/8byte.c b/src/bitfield/8byte.c
index 77251996..3b6b5466 100644
--- a/src/bitfield/8byte.c
+++ b/src/bitfield/8byte.c
@@ -40,16 +40,17 @@ uint64_t get_bit_field(uint64_t source, const uint16_t startBit,
return int_result;
}
-/**
- * TODO it would be nice to have a warning if you call with this a value that
- * won't fit in the number of bits you've specified it should use.
- */
-void set_bit_field(uint64_t* destination, uint64_t value, const uint16_t offset,
+bool set_bit_field(uint64_t* destination, uint64_t value, const uint16_t offset,
const uint16_t bit_count) {
+ if(value > bitmask(bit_count)) {
+ return false;
+ }
+
int shiftDistance = EIGHTBYTE_BIT - offset - bit_count;
value <<= shiftDistance;
*destination &= ~(bitmask(bit_count) << shiftDistance);
*destination |= value;
+ return true;
}
uint8_t nth_byte(const uint64_t source, const uint16_t byte_index) {