The assignment has us translate a Decimal number to Binary, and an operation that can be later invoked needs me to translate that binary back to decimal (cannot store the decimal number in a variable as a restriction).

The problem is the instructor has told us we can complete the operation of binary to decimal without the use of exponents (IE: powers of 2). I am relatively new to binary so I am unsure how to solve this without the use of exponents. Naturally the code has got to be O(N) but I fear the solution I have come up with, I think is O(N)^2 .

int Binary::convertToDecimal() { int total = 0; int power = 0; int count = bits-1; //bits is an int containing the total number of bits, used for array size while(count >= 0) { if(binArray[count] == true) //binArray is a bool array containing true for 1s, and 0 for false { total = total + powerUp(power); } power++; count--; } if(minus == true) //minus is a bool that is true if decimal number is negative { total = -total; } return total; } int Binary::powerUp(int power) { if(power == 0) { return 1; } else { int answer = 1; for(int i = 0; i < power; i++) { answer = answer * 2; } return answer; } }

Is there another way of converting Binary to decimal that I am not considering? Every site I've searched for on google ends up saying the use of exponents is the easiest way (the use of POW is restricted and I also cannot use longs or floats).