Let's note that the operands are integers. The fastest way to find the integer power
a
of 2 is
int power = 1 << a;
The method suggested by Wes Aday in his comment to the question is only good for floating point calculation; for integers it would involve double type cast — not good.
Please see:
http://www.cprogramming.com/tutorial/bitwise_operators.html[
^].
[EDIT]
But if the ultimate goal is to calculate the binary string representation, the solution is different. (Only not "decimal to binary": your integer values cannot be "decimal", only strings can be. You convert binary to binary strings, that is, extract bytes).
To find binary string, you need to extract each byte and fill in the string with 0 or 1. Here is how. First, create a bit mask, which should be a power of 2, see above. Do it in cycle for each bit. For a mask for each bit position, you have to find logical AND of the test number and the mask. The result is either zero or not. If zero, the text bit is clear, otherwise it is set:
char bitDigit;
if ((1 << bit) & value) == 0)
bitDigit = '0';
else
bitDigit = '1';
Fill values of
bitDigit
left to right in the string, according to the bit position
bit
, in cycle.
That's all.
—SA