Add byte printing macros
diff mbox

Message ID 1456326348-8754-1-git-send-email-msuraev@sysmocom.de
State Accepted
Headers show

Commit Message

Max Feb. 24, 2016, 3:05 p.m. UTC
From: Max <msuraev@sysmocom.de>

It's sometimes handy for debugging to be able to immediately see which
bits are set in a given byte. Generalize macro used for that in bitvec
tests and make it available for the rest of the library.
---
 include/osmocom/core/bits.h | 21 +++++++++++++++++++++
 tests/bitvec/bitvec_test.c  | 13 +------------
 2 files changed, 22 insertions(+), 12 deletions(-)

Patch
diff mbox

diff --git a/include/osmocom/core/bits.h b/include/osmocom/core/bits.h
index 1587b05..46f0c8b 100644
--- a/include/osmocom/core/bits.h
+++ b/include/osmocom/core/bits.h
@@ -50,6 +50,27 @@  int osmo_pbit2ubit_ext(ubit_t *out, unsigned int out_ofs,
                        const pbit_t *in, unsigned int in_ofs,
                        unsigned int num_bits, int lsb_mode);
 
+#define OSMO_BIN_SPEC "%d%d%d%d%d%d%d%d"
+#define OSMO_BIN_PRINT(byte)  \
+  (byte & 0x80 ? 1 : 0), \
+  (byte & 0x40 ? 1 : 0), \
+  (byte & 0x20 ? 1 : 0), \
+  (byte & 0x10 ? 1 : 0), \
+  (byte & 0x08 ? 1 : 0), \
+  (byte & 0x04 ? 1 : 0), \
+  (byte & 0x02 ? 1 : 0), \
+  (byte & 0x01 ? 1 : 0)
+
+#define OSMO_BIT_SPEC "%c%c%c%c%c%c%c%c"
+#define OSMO_BIT_PRINT(byte)  \
+  (byte & 0x80 ? '1' : '.'), \
+  (byte & 0x40 ? '1' : '.'), \
+  (byte & 0x20 ? '1' : '.'), \
+  (byte & 0x10 ? '1' : '.'), \
+  (byte & 0x08 ? '1' : '.'), \
+  (byte & 0x04 ? '1' : '.'), \
+  (byte & 0x02 ? '1' : '.'), \
+  (byte & 0x01 ? '1' : '.')
 
 /* BIT REVERSAL */
 
diff --git a/tests/bitvec/bitvec_test.c b/tests/bitvec/bitvec_test.c
index 800a040..76d6773 100644
--- a/tests/bitvec/bitvec_test.c
+++ b/tests/bitvec/bitvec_test.c
@@ -11,17 +11,6 @@ 
 #include <osmocom/core/bitvec.h>
 #include <osmocom/core/bits.h>
 
-#define BIN_PATTERN "%d%d%d%d%d%d%d%d"
-#define BIN(byte)  \
-  (byte & 0x80 ? 1 : 0), \
-  (byte & 0x40 ? 1 : 0), \
-  (byte & 0x20 ? 1 : 0), \
-  (byte & 0x10 ? 1 : 0), \
-  (byte & 0x08 ? 1 : 0), \
-  (byte & 0x04 ? 1 : 0), \
-  (byte & 0x02 ? 1 : 0), \
-  (byte & 0x01 ? 1 : 0)
-
 static char lol[1024]; // we pollute this with printed vectors
 static inline void test_rl(const struct bitvec *bv)
 {
@@ -45,7 +34,7 @@  static inline void test_get(struct bitvec *bv, unsigned n)
 	int16_t x = bitvec_get_int16_msb(bv, n);
 	uint8_t tmp[2];
 	osmo_store16be(x, &tmp);
-	printf(" -> %d (%u bit) ["BIN_PATTERN" "BIN_PATTERN"]:\n", x, n, BIN(tmp[0]), BIN(tmp[1]));
+	printf(" -> %d (%u bit) ["OSMO_BIN_SPEC" "OSMO_BIN_SPEC"]:\n", x, n, OSMO_BIN_PRINT(tmp[0]), OSMO_BIN_PRINT(tmp[1]));
 	bitvec_to_string_r(bv, lol);
 	printf("%s [%d]\n", lol, bv->cur_bit);
 }