Reversed Binary Numbers Here is my code for "Reverse Binary Numbers" in C.


  Problem
  
  Your task will be to write a program for reversing numbers in binary.
  For instance, the binary representation of 13 is 1101, and reversing
  it gives 1011, which corresponds to number 11.
  
  Input
  
  The input contains a single line with an integer \$N\$, (where
  \$1≤N≤1000000000\$ ).
  
  Output Output one line with one integer, the number we get by reversing the binary representation of \$N\$.
  
  Sample 1
  
  
  input 13
  output 11
  
  
  Sample 2
  
  
  input 47
  output 61
  


It's pretty efficient, but not the best. I wanted to improve the runtime. So I am looking for suggestions to improve the code.

Code

//Reverse Binary

#include &lt;stdlib.h&gt;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;

#define DIVISOR 2
#define MAX 50

int main() {
  int r, q, n, m = 1, num = 0, i = 0, len;
  int res[MAX];
  char str[MAX];

  scanf("%d", &amp;n);

  do {

    q = n / DIVISOR;
    r = n % DIVISOR;

    res[i] = r; 

    n = q;

    i++;

  } while(n &gt; 0);

  len = i;
  m = 1;

  for(; i &gt; 0; i--) {
    if(res[i - 1] != 0) {
      num += res[i - 1] * m;
    }
    m *= DIVISOR;
  }

  fprintf(stdout, "%d", num);

  return 0;
}
Division and multiplication are relatively computationally costly operations compared to shifts and AND operations.  Try to reformulate your answer in terms of &gt;&gt; and &amp; and you will likely find a performance gain.

Other things I noticed that may help you improve your program:

Eliminate unused variables

Both len and str are unused and may be eliminated from the program.

Try to put the loop exit condition at the top of the loop

Instead of using do {...} while, prefer to use while {...} or a for loop.  in your case this will also neatly solve the problem that occurs when the user types in a negative number.

return 0 is not needed at the end of main

Any modern compiler will automatically assume that getting to the end of main means that the program has executed successfully and will generate the equivalent of return 0 at the end.  It is not necessary to write it.

Check the return value of scanf

The call to scanf can fail, in which case it will return 0.  Your code should check for that.

If your input says you will only have positive numbers, use unsigned

Instead of "%d" it would make more sense to say scanf("%u", &amp;n) since the inputs cannot be negative.  Note that this will not prevent negative numbers from being input, but it makes the expectation clear within the code.  Alternatively, check for negative number input to make the code more robust.

Consider verifying that your data type will hold the maximum value

The input specification says that the input numbers are all in the range of \$1 \le N \le 1,000,000,000\$ but an int isn't necessarily large enough on all platforms.  Since it takes 30 bits to express that number, and because it's unsigned, it would probably make more sense to use a uint_fast32_t type defined in &lt;stdint.h&gt; which is a fast integer type containing at least 32 bits.