# Binary to Decimal Conversion without the use of exponents

Page 1 of 1

## 7 Replies - 3345 Views - Last Post: 03 October 2009 - 04:47 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=129670&amp;s=7a8f3766191edb57c953522a45068daa&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 jack9999

Reputation: 0
• Posts: 13
• Joined: 23-May 09

# Binary to Decimal Conversion without the use of exponents

Posted 03 October 2009 - 12:30 PM

Hi everyone, I'm working on an assignment and I am a little confused as to how to approach it.

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;
}
}

int Binary::powerUp(int power)
{
if(power == 0)
{
return 1;
}
else
{
for(int i = 0; i < power; i++)
{
}
}
}

```

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).

Is This A Good Question/Topic? 0

## Replies To: Binary to Decimal Conversion without the use of exponents

### #2 Aphex19

• Born again Pastafarian.

Reputation: 616
• Posts: 1,873
• Joined: 02-August 09

## Re: Binary to Decimal Conversion without the use of exponents

Posted 03 October 2009 - 01:55 PM

Im not sure how usefull this will be but i wrote this using QT.

```int MainWindow::convert_to_binary()
{
//clear the strings and lineEdits
ui->binary->clear();
binary_number.clear();
temp.clear();

decimal_number = ui->decimal->text().toInt();  //take input from user as an int

//loop while decimal_number is devisable by 2
while ((float)decimal_number > 1)
{
//if input is even
if (decimal_number % 2 == 0)
{
binary_number.append("0");  //Next binary number is a zero
decimal_number = decimal_number / 2;  //devide decimal_number by 2
}

//if input is odd
else
{
binary_number.append("1");  //Next binary number is a one
decimal_number = decimal_number / 2; //devide decimal_number by 2
}
}

//reverse text as the string is
//back to front using this method
//of decimal to binary conversion
counter = binary_number.length() -1; //minus 1 otherwise there was a empty space before the string

counter_start_value = counter; //remember original value of counter in temp2 before modification

for(;i <= counter_start_value;i ++) //loop while "i" is <= length of string, increment "i" by one.
{
temp.insert(i, binary_number[counter --]);  //insert "binary_number[counter --]" into "temp"
}											   //at position "i", this reverses the string as
//as counter is decrementing

//then finally output the result
ui->binary->setText(temp);

decimal_number = 0,counter = 0, i = 0, counter_start_value = 0; //avoids problems at runtime

return 0;
}

```

Took me a while but it works

### #3 jack9999

Reputation: 0
• Posts: 13
• Joined: 23-May 09

## Re: Binary to Decimal Conversion without the use of exponents

Posted 03 October 2009 - 02:01 PM

I'm afraid thats not what I needed What you have there is a Decimal to Binary converter, which I already have and is working fine. I need an alternative method of converting binary to decimal that somehow does not use the power of 2 method.

### #4 Aphex19

• Born again Pastafarian.

Reputation: 616
• Posts: 1,873
• Joined: 02-August 09

## Re: Binary to Decimal Conversion without the use of exponents

Posted 03 October 2009 - 02:25 PM

I see, sorry i should have paid more attention.

Its kinda tough without the power of two method and especially hard to implement in code. here is a method from this site

Doubling method

Starting from zero, and working from left to right, double your number and add the next digit of the base two representation. For example to convert 1011001, we take the following steps.

1. 1|011001 0*2+1 = 1
2. 10|11001 1*2+0 = 2
3. 101|1001 2*2+1 = 5
4. 1011|001 5*2+1 = 11
5. 10110|01 11*2+0 = 22
6. 101100|1 22*2+0 = 44
7. 1011001 44*2+1 = 89

Hope this helps.

### #5 jack9999

Reputation: 0
• Posts: 13
• Joined: 23-May 09

## Re: Binary to Decimal Conversion without the use of exponents

Posted 03 October 2009 - 03:03 PM

Aphex19, on 3 Oct, 2009 - 01:25 PM, said:

I see, sorry i should have paid more attention.

Its kinda tough without the power of two method and especially hard to implement in code. here is a method from this site

Doubling method

Starting from zero, and working from left to right, double your number and add the next digit of the base two representation. For example to convert 1011001, we take the following steps.

1. 1|011001 0*2+1 = 1
2. 10|11001 1*2+0 = 2
3. 101|1001 2*2+1 = 5
4. 1011|001 5*2+1 = 11
5. 10110|01 11*2+0 = 22
6. 101100|1 22*2+0 = 44
7. 1011001 44*2+1 = 89

Hope this helps.

This looks promising, I'm going to work on the coding for this for a bit and let you know how it turns out.

### #6 jack9999

Reputation: 0
• Posts: 13
• Joined: 23-May 09

## Re: Binary to Decimal Conversion without the use of exponents

Posted 03 October 2009 - 03:12 PM

...and it works! This is a much simpler method to code for than my previous attempt! What took me 25 lines and another function to achieve something that sounded like i was using exponentials, I've recoded into 10 lines in 1 function!

Thanks a lot!

### #7 Aphex19

• Born again Pastafarian.

Reputation: 616
• Posts: 1,873
• Joined: 02-August 09

## Re: Binary to Decimal Conversion without the use of exponents

Posted 03 October 2009 - 03:23 PM

You program fast aswell :0

### #8 carltech

• What did you call me?

Reputation: 28
• Posts: 997
• Joined: 19-October 07

## Re: Binary to Decimal Conversion without the use of exponents

Posted 03 October 2009 - 04:47 PM

I'm not sure if it's too late or if you have to do it arithmatically but you can do it with Bitset.