Before I forget about it again, someone reported an issue with the code I posted a LONG time ago on this thread: Potentially useful code
but I forgot to take a look at it, well now I have and have fixed the issue, for the sake of forcing the existance of macros like INT_DIG this code can be used/referenced, detecting the bits will need an alternative macro though.
Code:
#include <stdio.h>
#include <limits.h>
#include <inttypes.h>
typedef unsigned int ui_t;
typedef long long ll_t;
typedef unsigned long long ull_t;
#define B10_MOD10(BITS) ((BITS) % 10)
#define B10_DIV10(BITS) (((BITS) / 10) * 3)
#define B10_BIT2DIG(BITS) ( B10_DIV10(BITS) + \
((B10_MOD10(BITS) == 0) ? (B10_DIV10(BITS) != 0) : \
((B10_MOD10(BITS)/3) + ((B10_MOD10(BITS)%3)!=0)) ))
ui_t base10_bit2dig( ui_t bits ) {
ui_t a = bits % 10;
ui_t b = (bits / 10) * 3;
return b + ((a == 0) ? (b != 0) : ((a/3) + ((a%3) != 0)));
}
int main() {
static ui_t const cap = (sizeof(ll_t)*CHAR_BIT);
ui_t i = 0u, d, e;
ull_t v = 0u;
while ( i < cap ) {
d = base10_bit2dig(i);
e = B10_BIT2DIG(i);
printf( "BITS: %02u; DIGS: %02u; EDIG: %02u; VAL: %llu\n", i++, d, e, v );
v <<= 1;
v |= 1u;
}
return 0;
}