top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to print a number in binary form in C/C++?

+4 votes
433 views

I am trying to do something like this

char a = 10;
printf("binary representation of a = %b",a);

and expecting

0x1010
posted Jan 7, 2014 by Amit Mishra

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

You dont have a option like %x for binary numbers so you need to write a function to print or you can create a string and just print that. Try something like

void bin(unsigned n)
{
    unsigned i;
    for (i = 1 << 31; i > 0; i = i / 2)
        (n & i)? printf("1"): printf("0");
}
answer Jan 7, 2014 by Deepti Singh
Similar Questions
+1 vote

How to balance the binary search tree in C/C++? Please provide the necessary explanation also?

+2 votes

How to find the smallest element in a Binary Tree (Not BST)? Sample C/C++ code would be helpful.

+4 votes

For ex : Given post order sequence 1 3 2 6 9 8 5
its pre order sequence is 5 2 1 3 8 6 9

+2 votes

Please share the code or algorithm to delete a node from Threaded Binary Tree.

...