-3

For example 0x19 is 00011001 in binary. I tried using printf %08x, but that gives me 00000019 as the output. How do I get it to print 00011001 instead?

2 Answers2

2
for (i=0; i<32; i++) putchar((x&(1<<(31-i)))?'1':'0');
Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
0

If itoa(non-standard function) can be used environment, can be written as follows.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>

int main( void ){
    char bits[CHAR_BIT*sizeof(unsigned)+1];
    itoa((int)0x19, bits, 2);
    int len = strlen(bits);
    if(len < 8)//%08
        printf("%0*d%s\n", 8-len, 0, bits);
    else
        printf("%s\n", bits);
    return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70