# How To Convert Binary to Decimal

Page 1 of 1

## 8 Replies - 3461 Views - Last Post: 07 July 2009 - 09:34 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=113383&amp;s=ea411d49cd045b53f5891bc9ceafe68c&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 mamme

Reputation: 0
• Posts: 5
• Joined: 11-July 08

# How To Convert Binary to Decimal

Posted 06 July 2009 - 11:07 AM

```#include <iostream>
using namespace std;

#define b(n) ((x>>(n))&0x01)

void main()
{
int i,j,x,y1,y2,y3,b[10];
for(i=0; i<10; i++)
{
b[i] = 1<<i;
}

x = 372; // seed number

for(j=0;j<10;j++)
{
//rotate x to the left, and copy the exclusive or of bits 9, 8 and 0 into bit 0:
y1 = (b[9] & x) ? 1 : 0;
y2 = (b[8] & x) ? 1 : 0;
y3 = (b[0] & x) ? 1 : 0;
x = x << 1 | (y1^y2^y3);

// show result
for(int i=0;i<10;i++)
{
cout<<b(10-i)<<" ";
}
cout<<endl;
}

}

```

output::

hi all i have problem with 10 bits LFSR random number generator in c++

for result number is binary i want show number as decimal

thank all

Is This A Good Question/Topic? 0

## Replies To: How To Convert Binary to Decimal

### #2 Dantheman

• D.I.C Regular

Reputation: 34
• Posts: 445
• Joined: 27-May 09

## Re: How To Convert Binary to Decimal

Posted 06 July 2009 - 11:16 AM

1011 in binary is = 1 * 1 + 1 * 2 + 0 * 4 + 1 * 8 = 11. Figure out the pattern and write a program that implements it.

This post has been edited by Dantheman: 06 July 2009 - 11:17 AM

### #3 BetaWar

• #include "soul.h"

Reputation: 1513
• Posts: 8,290
• Joined: 07-September 06

## Re: How To Convert Binary to Decimal

Posted 06 July 2009 - 11:56 AM

Binary to Decimal conversion is pretty simple. All you need to do is get the last digit from the binary number and then multiply it by 2 raised to its (the digit's) position in the binary number.

```#include <cmath>
#include <iostream>
using namespace std;

int main(){
int bin = 1011;
int dec = 0;
int tmp = 0;
for(float i=0; bin > 0; i++){
tmp = (int)((float)(((float)bin/10)-(bin/10))*10);
dec += tmp*pow(2, i);
bin /= 10;
}
cout << "1011 = " << dec << endl;
}
```

There is a simple conversion program. Hope it helps.

### #4 Dantheman

• D.I.C Regular

Reputation: 34
• Posts: 445
• Joined: 27-May 09

## Re: How To Convert Binary to Decimal

Posted 06 July 2009 - 01:29 PM

Wow, so long for the forum guidelines I guess. I have a feeling that some people post those guidelines simply because they don't know the answer to the question.

But, I guess the leecher got what he wanted, so I'll post the code. Betawar, your code uses pow() call, some weird float casts, a float counter, and a bunch of weird operations, so I think my code would be a bit easier to understand:

Edit: Actually, your code doesn't even work. So the leecher didn't get what he wanted after all. Good. In that case I am removing my code too.

@mamme: Either read my first post and figure out the algorithm or use Google. If you have a specific question and code to show us that you're trying to figure it out, please ask and we will help.

This post has been edited by Dantheman: 06 July 2009 - 01:54 PM

### #5 baavgai

• Dreaming Coder

Reputation: 7181
• Posts: 14,969
• Joined: 16-October 07

## Re: How To Convert Binary to Decimal

Posted 06 July 2009 - 03:06 PM

BetaWar, on 6 Jul, 2009 - 12:56 PM, said:

```tmp = (int)((float)(((float)bin/10)-(bin/10))*10);
dec += tmp*pow(2, i);

```

Whoa. How'd you get from pretty simple to pow and float and...

To the OP, please do your own work. There two basic methods, a mathematical double or a simple bit shift. Doubtless your instructor would like to see the bit shift version. You can find it easily enough, but think about it.
```101
0x00 + 1 = 0x01
0x01 << 1  = 0x02
0x02 + 0  = 0x02
0x02 << 1  = 0x04
0x04 + 1 = 0x05

```

Hope this helps. Good luck.

### #6 Dantheman

• D.I.C Regular

Reputation: 34
• Posts: 445
• Joined: 27-May 09

## Re: How To Convert Binary to Decimal

Posted 06 July 2009 - 03:27 PM

Baavgai, I think you chose a rather confusing example which may lead to some confusion. You've picked a palindrome number 101 and people might not understand from which point are you reading a number. Are you doing it from left to right or from right to left? Can't really really tell when it's a palindrome

So just to clear the confusion, the number is being read from left to right, NOT the other way (although here it worked just fine ). So another example would be:

```1011
0x00 + 1 = 0x01
0x01 << 1  = 0x02
0x02 + 0 = 0x02
0x02 << 1 = 0x04
0x04 + 1 = 0x05
0x05 << 1 = 0xA
0x0A + 1 = 0x0B

```

### #7 BetaWar

• #include "soul.h"

Reputation: 1513
• Posts: 8,290
• Joined: 07-September 06

## Re: How To Convert Binary to Decimal

Posted 06 July 2009 - 04:24 PM

Quote

Actually, your code doesn't even work

I am not sure what you tested in, but it works fine for me in Visual C++ 2008 Express Edition for everything I have tried. 1101 = 11, 111 = 7, 111011 = 59, 1 = 1, 11 = 3, 1101 = 13.

Quote

How'd you get from pretty simple to pow and float and...

Ya, not the prettiest solution, but it was the first one I thought of.

### #8 mamme

Reputation: 0
• Posts: 5
• Joined: 11-July 08

## Re: How To Convert Binary to Decimal

Posted 07 July 2009 - 07:19 AM

```#include <iostream>
using namespace std;

#define b(n) ((x>>(n))&0x01)

void main()
{
int i,j,x,y1,y2,y3,b[11];
int count=0;
for(i=1; i<11; i++)
{
b[i] = 1<<i;
}

x = 127; // seed number
for(j=0;j<100;j++)
{
cout<<"X("<<count<<") = "<<x<<" = ";
y1 = (b[10] & x) ? 1 : 0; // tap at bit 10
y2 = (b[9] & x) ? 1 : 0;  // tap at bit 9
y3 = (b[5] & x) ? 1 : 0;  // tap at bit 5
x = x << 1 | (y1^y2^y3); //  bit 10 XOR bit 9 XOR bit 5 move value at bit 1  and Rotate x to Left
// if random number > 1000 mod 1000
if(x>999)
{
x=x%1000;
}
// Show Result
for(int i=1;i<11;i++)
{
cout<<b(11-i)<<" ";
}
count++;
cout<<endl;
}
}

```

hi thank everyone for help me

i found problem for 10 bit LFSR is value must not more 1024 = 2^10

let number random > 1000 mod 1000

and show x before rotate will get decimal number

i've completed thank all

output

This post has been edited by mamme: 07 July 2009 - 01:05 PM

### #9 Dаntheman

Reputation: 0
• Posts: 3
• Joined: 07-July 09

## Re: How To Convert Binary to Decimal

Posted 07 July 2009 - 09:34 AM

BetaWar, on 6 Jul, 2009 - 03:24 PM, said:

I am not sure what you tested in, but it works fine for me in Visual C++ 2008 Express Edition for everything I have tried.

I tested it with GCC. Then you said that it worked for you and I tested it with Dev-C++, but still got the same output. It's interesting that this thing works in VC++. I bet you a hundred bucks that casting like that is not part of a standard, so different compilers act differently to such code.