An Interest In:
Web News this Week
- April 29, 2024
- April 28, 2024
- April 27, 2024
- April 26, 2024
- April 25, 2024
- April 24, 2024
- April 23, 2024
Some of Our Sources
View All Sources
April 5, 2023 05:38 pm GMT
Original Link: https://dev.to/pauljlucas/bit-constant-macros-in-c-42n9
Bit Constant Macros in C
Introduction
In addition to my bit testing functions in C, here are some bit constant macros in C. (These are used as part of cdecl.)
The Macros
The first macro, given an integer having exactly one bit set, returns a value where all bits less than that are set, e.g., given 0b00010000
, returns 0b00001111
:
#define BITS_LT(N) ((N) - 1u)
Given that definition, a macro that returns a value where all bits that are less than or equal to the one set bit is:
#define BITS_LE(N) (BITS_LT(N) | (N))
Finally, the greater-than counterparts are trivially:
#define BITS_GT(N) (~BITS_LE(N))#define BITS_GE(N) (~BITS_LT(N))
Original Link: https://dev.to/pauljlucas/bit-constant-macros-in-c-42n9
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To