Created
August 15, 2025 05:44
-
-
Save hidsh/a011d538fc102c764904160123932ef4 to your computer and use it in GitHub Desktop.
c example to use `FIELD_GET()` macro from zephyr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdint.h> | |
#include <stdio.h> | |
// zephyr-latest/zephyr-proj/zephyr/include/zephyr/sys/util_macro.h | |
/** @brief Extract the Least Significant Bit from @p value. */ | |
#define LSB_GET(value) ((value) & -(value)) | |
/** | |
* @brief Extract a bitfield element from @p value corresponding to | |
* the field mask @p mask. | |
*/ | |
#define FIELD_GET(mask, value) (((value) & (mask)) / LSB_GET(mask)) | |
int main(void) | |
{ | |
uint8_t val = 0xc4; | |
printf("val=0x%X\n", val); | |
for (int bit_num=7; bit_num>=0; bit_num--) { | |
uint8_t mask = (1 << bit_num); | |
printf("bit %d = %X\n", bit_num, FIELD_GET(mask, val)); | |
} | |
} | |
/* | |
val=0xC4 | |
bit 7 = 1 | |
bit 6 = 1 | |
bit 5 = 0 | |
bit 4 = 0 | |
bit 3 = 0 | |
bit 2 = 1 | |
bit 1 = 0 | |
bit 0 = 0 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment