How to print binary number via printf

318,116

Solution 1

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");

Solution 2

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);

Here's the origin:

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.

Share:
318,116

Related videos on Youtube

Registered User
Author by

Registered User

Never let the fear of falling down keep you away from playing the game

Updated on December 26, 2020

Comments

  • Registered User
    Registered User almost 2 years

    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?

    • cnicutar
      cnicutar over 11 years
    • Kerrek SB
      Kerrek SB over 11 years
      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.
    • Vinicius Kamakura
      Vinicius Kamakura over 11 years
      @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
    • Kerrek SB
      Kerrek SB over 11 years
      @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.
    • C.D.H.
      C.D.H.
      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.
  • David Heffernan
    David Heffernan about 10 years
    This prints the binary representation backwards
  • kapilddit
    kapilddit about 10 years
    @David Heffernan How can we write which support both plateform 32bit and 64bit.
  • kapilddit
    kapilddit about 10 years
    @hexa make function & use recursion to correct the printing order.will it be helpful?
  • evandrix
    evandrix about 9 years
    in case <stdlib.h>/<cstdlib> is not working for you, here's a quick roll-your-own implementation @ strudel.org.uk/itoa
  • jww
    jww almost 8 years
    @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.
  • danijar
    danijar almost 8 years
    Corrected via recursion stackoverflow.com/a/27627015/1079110.
  • luart
    luart over 7 years
    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: stackoverflow.com/a/31660310/1814353
  • brita_
    brita_ over 6 years
    For correct order use and any type use: template <typename T> void print_binary(T n) { int numbits = sizeof(T) * 8; while(--numbits >= 0) printf("%c", (n & ((T)1<<numbits)) ? '1' : '0'); } For C only with an hardcoded data type, replace all 'T' by eg.'int'.
  • choppe
    choppe over 1 year
    if n is not an unsigned integer, this loop will print for ever.
  • 71GA
    71GA 10 months
    Note that printf() in this case is an extremely large overhead. You should instead use putc(). It will greatly increase performance.

Related