From 67996073ccd956ba78b201da74e93a21e6c6c68b Mon Sep 17 00:00:00 2001 From: DimitryP6 Date: Sun, 1 Dec 2024 17:22:57 -0500 Subject: [PATCH] #200: created macros for adding and setting bits --- middleware/include/c_utils.h | 44 ++++++++++++++++++++++-------------- middleware/src/c_utils.c | 38 +++++++++++++++---------------- 2 files changed, 46 insertions(+), 36 deletions(-) diff --git a/middleware/include/c_utils.h b/middleware/include/c_utils.h index 832fe17..e28b01e 100644 --- a/middleware/include/c_utils.h +++ b/middleware/include/c_utils.h @@ -1,18 +1,28 @@ -#ifndef C_UTILS -#define C_UTILS - -/* - * Will retrieve the container of a certain pointer given the container type and - * its pointer Trick pulled from Linux Kernel - */ -#define CONTAINER_OF(ptr, type, member) \ - ((type *)((char *)(ptr) - offsetof(type, member))) - -#endif /* C_UTILS */ - -void endian_swap(void *ptr, int size); - -/// @brief Reverses the bit order of a byte -/// @param b byte -/// @return the same byte but wuth the bits reversed +#ifndef C_UTILS +#define C_UTILS + +/* + * Will retrieve the container of a certain pointer given the container type and + * its pointer Trick pulled from Linux Kernel + */ +#define CONTAINER_OF(ptr, type, member) \ + ((type *)((char *)(ptr) - offsetof(type, member))) + +/* + * Gets a bit of a binary number at desired location +*/ +#define GET_BIT(num, bit) ((num >> bit) & 1) + +/* + * Sets a bit of a binary number at desired location +*/ +#define SET_BIT(num, bit) (num |= (1UL << bit)) + +#endif /* C_UTILS */ + +void endian_swap(void *ptr, int size); + +/// @brief Reverses the bit order of a byte +/// @param b byte +/// @return the same byte but wuth the bits reversed unsigned char reverse_bits(unsigned char b); \ No newline at end of file diff --git a/middleware/src/c_utils.c b/middleware/src/c_utils.c index f0524f4..7944d43 100644 --- a/middleware/src/c_utils.c +++ b/middleware/src/c_utils.c @@ -1,20 +1,20 @@ -#include "c_utils.h" - -void endian_swap(void *ptr, int size) -{ - char *p = (char *)ptr; - char tmp; - for (int i = 0; i < size / 2; i++) { - tmp = p[i]; - p[i] = p[size - i - 1]; - p[size - i - 1] = tmp; - } -} - -unsigned char reverse_bits(unsigned char b) -{ - b = (b & 0xF0) >> 4 | (b & 0x0F) << 4; - b = (b & 0xCC) >> 2 | (b & 0x33) << 2; - b = (b & 0xAA) >> 1 | (b & 0x55) << 1; - return b; +#include "c_utils.h" + +void endian_swap(void *ptr, int size) +{ + char *p = (char *)ptr; + char tmp; + for (int i = 0; i < size / 2; i++) { + tmp = p[i]; + p[i] = p[size - i - 1]; + p[size - i - 1] = tmp; + } +} + +unsigned char reverse_bits(unsigned char b) +{ + b = (b & 0xF0) >> 4 | (b & 0x0F) << 4; + b = (b & 0xCC) >> 2 | (b & 0x33) << 2; + b = (b & 0xAA) >> 1 | (b & 0x55) << 1; + return b; } \ No newline at end of file