aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--include/grpc/support/useful.h9
-rw-r--r--test/core/support/useful_test.c7
2 files changed, 16 insertions, 0 deletions
diff --git a/include/grpc/support/useful.h b/include/grpc/support/useful.h
index e1ce0455c6..6eb212a311 100644
--- a/include/grpc/support/useful.h
+++ b/include/grpc/support/useful.h
@@ -52,4 +52,13 @@
b = x; \
} while (0)
+/** Set the \a n-th bit of \a i */
+#define GPR_BITSET(i, n) (i |= (1u << n))
+
+/** Clear the \a n-th bit of \a i */
+#define GPR_BITCLEAR(i, n) (i &= ~(1u << n))
+
+/** Get the \a n-th bit of \a i */
+#define GPR_BITGET(i, n) ((i & (1u << n)) != 0)
+
#endif /* GRPC_SUPPORT_USEFUL_H */
diff --git a/test/core/support/useful_test.c b/test/core/support/useful_test.c
index feaf436379..0804a81b7c 100644
--- a/test/core/support/useful_test.c
+++ b/test/core/support/useful_test.c
@@ -39,6 +39,7 @@
int main(int argc, char **argv) {
int four[4];
int five[5];
+ gpr_uint32 bitset = 0;
grpc_test_init(argc, argv);
GPR_ASSERT(GPR_MIN(1, 2) == 1);
@@ -55,5 +56,11 @@ int main(int argc, char **argv) {
GPR_ASSERT(GPR_ARRAY_SIZE(four) == 4);
GPR_ASSERT(GPR_ARRAY_SIZE(five) == 5);
+ GPR_ASSERT(GPR_BITSET(bitset, 3) == 8);
+ GPR_ASSERT(GPR_BITGET(bitset, 3) == 1);
+ GPR_ASSERT(GPR_BITSET(bitset, 1) == 10);
+ GPR_ASSERT(GPR_BITCLEAR(bitset, 3) == 2);
+ GPR_ASSERT(GPR_BITGET(bitset, 3) == 0);
+
return 0;
}