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
This commit is contained in:
Martine Lenders 2019-11-20 18:45:58 +01:00
parent a2b72f4012
commit a6623f834f

View File

@ -10,6 +10,10 @@
* @defgroup sys_bitfield Bitfields * @defgroup sys_bitfield Bitfields
* @ingroup sys * @ingroup sys
* @brief Bitfields of arbitrary length * @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 * @file
* @{ * @{
* *
@ -49,7 +53,7 @@ extern "C" {
*/ */
static inline void bf_set(uint8_t field[], size_t idx) 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) 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) 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) 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))));
} }
/** /**