From a6623f834f3d7af12f1557aba0745a40597f29df Mon Sep 17 00:00:00 2001 From: Martine Lenders Date: Wed, 20 Nov 2019 18:45:58 +0100 Subject: [PATCH] bitfield: unify order Currently the bitfield type mixes up the order of bits: While the byte order is big-endian (most significant byte first), the bit order of each single byte within the bitfield is little-endian (most significant bit last). While this isn't a problem for most applications of the bitfield type it becomes one when sending the bitfield over the network (as done e.g. in the [ACKs of Selective Fragment Recovery][SFR-ACKs]). This change unifies byte order and bit order to both be most significant bX first. [SFR-ACKs]: https://tools.ietf.org/html/draft-ietf-6lo-fragment-recovery-07 --- sys/include/bitfield.h | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/sys/include/bitfield.h b/sys/include/bitfield.h index a0e89acd62..73225ef14f 100644 --- a/sys/include/bitfield.h +++ b/sys/include/bitfield.h @@ -10,6 +10,10 @@ * @defgroup sys_bitfield Bitfields * @ingroup sys * @brief Bitfields of arbitrary length + * + * The bitfields in this module have their most significant bytes first and + * their most significant bits within each byte of the bitfield also first. + * * @file * @{ * @@ -49,7 +53,7 @@ extern "C" { */ static inline void bf_set(uint8_t field[], size_t idx) { - field[idx / 8] |= (1u << (idx % 8)); + field[idx / 8] |= (1u << (7 - (idx % 8))); } /** @@ -60,7 +64,7 @@ static inline void bf_set(uint8_t field[], size_t idx) */ static inline void bf_unset(uint8_t field[], size_t idx) { - field[idx / 8] &= ~(1u << (idx % 8)); + field[idx / 8] &= ~(1u << (7 - (idx % 8))); } /** @@ -71,7 +75,7 @@ static inline void bf_unset(uint8_t field[], size_t idx) */ static inline void bf_toggle(uint8_t field[], size_t idx) { - field[idx / 8] ^= (1u << (idx % 8)); + field[idx / 8] ^= (1u << (7 - (idx % 8))); } /** @@ -82,7 +86,7 @@ static inline void bf_toggle(uint8_t field[], size_t idx) */ static inline bool bf_isset(uint8_t field[], size_t idx) { - return (field[idx / 8] & (1u << (idx % 8))); + return (field[idx / 8] & (1u << (7 - (idx % 8)))); } /**