AP_Common: added missing bit set/clear functions

This commit is contained in:
yaapu 2020-08-04 22:45:52 +02:00 committed by Andrew Tridgell
parent 9efc4602af
commit 6a0531c08a
1 changed files with 15 additions and 0 deletions

View File

@ -22,6 +22,7 @@
#include <stdint.h>
#include <stdlib.h>
#include <type_traits>
// used to pack structures
#define PACKED __attribute__((__packed__))
@ -143,3 +144,17 @@ bool hex_to_uint8(uint8_t a, uint8_t &res); // return the uint8 value of an asc
strncpy without the warning for not leaving room for nul termination
*/
void strncpy_noterm(char *dest, const char *src, size_t n);
/*
Bit manipulation
*/
//#define BIT_SET(value, bitnumber) ((value) |= (((typeof(value))1U) << (bitnumber)))
template <typename T> void BIT_SET (T& value, uint8_t bitnumber) noexcept {
static_assert(std::is_integral<T>::value, "Integral required.");
((value) |= ((T)(1U) << (bitnumber)));
}
//#define BIT_CLEAR(value, bitnumber) ((value) &= ~(((typeof(value))1U) << (bitnumber)))
template <typename T> void BIT_CLEAR (T& value, uint8_t bitnumber) noexcept {
static_assert(std::is_integral<T>::value, "Integral required.");
((value) &= ~((T)(1U) << (bitnumber)));
}