48

Possible Duplicate:
Is there a printf converter to print in binary format?

Here is my program

#include<stdio.h>
int main ()
{
    int i,a=2;
    i=~a;
    printf("a=%d\ni=%d\n",a,i);

    return 0;
}

The output is

a=2
i=-3

I want this to print in binary. There are %x, %o, and %d which are for hexadecimal, octal, and decimal number, but what is for printing binary in printf?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Registered User
  • 5,173
  • 16
  • 47
  • 73
  • 6
    http://stackoverflow.com/questions/111928/is-there-a-printf-converter-to-print-in-binary-format – cnicutar Jun 16 '11 at 13:58
  • 2
    Do you really want binary? Hexadecimal is often just as good (or even better), as it maps every 4 bits into one hex-digit, giving you both a compact and expressive representation of the binary data. – Kerrek SB Jun 16 '11 at 14:01
  • 3
    @Kerrek are you really saying that seeing a number in it's binary representation is useless? Try analyzing a float number in hex digits :P – Vinicius Kamakura Jun 16 '11 at 14:19
  • 2
    @hexa: Yep, doing that all the time. I wrote a ULP comparer for long doubles, which I gladly debugged in hex. Hex really **is** just binary compressed a little. – Kerrek SB Jun 16 '11 at 14:22
  • @Kerrek I just don't have that brain power :) – Vinicius Kamakura Jun 16 '11 at 14:27
  • @hexa: It's really not so hard, you just have to remember 16 combinations... 0x0 is 0000b, and 0x8 is 1000b, those are the most important ones... better than having a screen filled with zeros and ones! :-) It's all just a matter of being used to it, but I'm sure you'd pick it up in no time if you set your mind to it. – Kerrek SB Jun 16 '11 at 14:29
  • 1
    binary can be useful for looking at how bitwise memory maps are set, if they are documented accordingly and you want to look at the values laid out the same way as in the document. lets not bust anyone's chops for wanting their data in whatever format suits their needs most. – C.D.H. Sep 04 '19 at 21:30

2 Answers2

59

printf() doesn't directly support that. Instead you have to make your own function.

Something like:

while (n) {
    if (n & 1)
        printf("1");
    else
        printf("0");

    n >>= 1;
}
printf("\n");
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Vinicius Kamakura
  • 7,665
  • 1
  • 29
  • 43
  • 98
    This prints the binary representation backwards – David Heffernan Sep 30 '12 at 19:44
  • @David Heffernan How can we write which support both plateform 32bit and 64bit. – kapilddit Oct 19 '12 at 06:02
  • 1
    @hexa make function & use recursion to correct the printing order.will it be helpful? – kapilddit Oct 19 '12 at 06:07
  • @kapilddit - why in the world would you try to add your answer to Hexa's answer? You've been around long enough to know how Stack Overflow works. – jww Dec 22 '14 at 06:01
  • Corrected via recursion http://stackoverflow.com/a/27627015/1079110. – danijar Dec 23 '14 at 19:47
  • Outlined conversion can't be used multiple times inside complex printf() and is not efficient (multiple calls to printf). Here is one statement standard and generic solution: http://stackoverflow.com/a/31660310/1814353 – luart Jul 27 '15 at 18:26
  • For correct order use and any type use: `template void print_binary(T n) { int numbits = sizeof(T) * 8; while(--numbits >= 0) printf("%c", (n & ((T)1< – brita_ Mar 31 '16 at 13:12
  • if n is not an unsigned integer, this loop will print for ever. – choppe Aug 25 '21 at 14:14
  • Note that `printf()` in this case is an extremely large overhead. You should instead use `putc()`. It will greatly increase performance. – 71GA Mar 08 '22 at 07:52
53

Although ANSI C does not have this mechanism, it is possible to use itoa() as a shortcut:

char buffer[33];
itoa(i, buffer, 2);
printf("binary: %s\n", buffer);

Source: itoa() in cplusplus reference

It is non-standard C, but K&R mentioned the implementation in the C book, so it should be quite common. It should be in stdlib.h.

user16217248
  • 3,119
  • 19
  • 19
  • 37
zw324
  • 26,764
  • 16
  • 85
  • 118
  • 2
    in case / is not working for you, here's a quick roll-your-own implementation @ http://www.strudel.org.uk/itoa – evandrix Oct 18 '13 at 17:02