patterncModerate
Reversed Binary Numbers
Viewed 0 times
binarynumbersreversed
Problem
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
Sample 2
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
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
#include
#include
#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", &n);
do {
q = n / DIVISOR;
r = n % DIVISOR;
res[i] = r;
n = q;
i++;
} while(n > 0);
len = i;
m = 1;
for(; i > 0; i--) {
if(res[i - 1] != 0) {
num += res[i - 1] * m;
}
m *= DIVISOR;
}
fprintf(stdout, "%d", num);
return 0;
}Solution
Division and multiplication are relatively computationally costly operations compared to shifts and
Other things I noticed that may help you improve your program:
Eliminate unused variables
Both
Try to put the loop exit condition at the top of the loop
Instead of using
Any modern compiler will automatically assume that getting to the end of
Check the return value of
The call to
If your input says you will only have positive numbers, use
Instead of
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
AND operations. Try to reformulate your answer in terms of >> and & 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 mainAny 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
scanfThe 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
unsignedInstead of
"%d" it would make more sense to say scanf("%u", &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 `` which is a fast integer type containing at least 32 bits.Context
StackExchange Code Review Q#75817, answer score: 13
Revisions (0)
No revisions yet.