@@ -131,4 +131,11 @@ static inline bool is_power_of_2(unsigned long n)
return n && !(n & (n - 1));
}
+/*
+ * One byte per bit, a ' between each group of 4 bits, and a null terminator.
+ */
+#define BINSTR_SZ (sizeof(unsigned long) * 8 + sizeof(unsigned long) * 2)
+void binstr(unsigned long x, char out[BINSTR_SZ]);
+void print_binstr(unsigned long x);
+
#endif
@@ -259,3 +259,33 @@ int printf(const char *fmt, ...)
puts(buf);
return r;
}
+
+void binstr(unsigned long x, char out[BINSTR_SZ])
+{
+ int i;
+ char *c;
+ int n;
+
+ n = sizeof(unsigned long) * 8;
+ i = 0;
+ c = &out[0];
+ for (;;) {
+ *c++ = (x & (1ul << (n - i - 1))) ? '1' : '0';
+ i++;
+
+ if (i == n) {
+ *c = '\0';
+ break;
+ }
+ if (i % 4 == 0)
+ *c++ = '\'';
+ }
+ assert(c + 1 - &out[0] == BINSTR_SZ);
+}
+
+void print_binstr(unsigned long x)
+{
+ char out[BINSTR_SZ];
+ binstr(x, out);
+ printf("%s", out);
+}