8 Replies - 2693 Views - Last Post: 07 July 2009 - 09:34 AM Rate Topic: -----

#1 mamme  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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];
  //First build 10 mask bits:
	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::

Posted Image

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  Icon User is offline

  • D.I.C Regular

Reputation: 34
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#3 BetaWar  Icon User is online

  • #include "soul.h"
  • member icon

Reputation: 1148
  • View blog
  • Posts: 7,144
  • 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.
Was This Post Helpful? 0
  • +
  • -

#4 Dantheman  Icon User is offline

  • D.I.C Regular

Reputation: 34
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5823
  • View blog
  • Posts: 12,675
  • Joined: 16-October 07

Re: How To Convert Binary to Decimal

Posted 06 July 2009 - 03:06 PM

View PostBetaWar, on 6 Jul, 2009 - 12:56 PM, said:

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



:blink: 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.
Was This Post Helpful? 0
  • +
  • -

#6 Dantheman  Icon User is offline

  • D.I.C Regular

Reputation: 34
  • View blog
  • 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 :D

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


Was This Post Helpful? 0
  • +
  • -

#7 BetaWar  Icon User is online

  • #include "soul.h"
  • member icon

Reputation: 1148
  • View blog
  • Posts: 7,144
  • 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.
Was This Post Helpful? 0
  • +
  • -

#8 mamme  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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

Posted Image

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

Was This Post Helpful? 0
  • +
  • -

#9 Dаntheman  Icon User is offline

  • New D.I.C Head

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

Re: How To Convert Binary to Decimal

Posted 07 July 2009 - 09:34 AM

View PostBetaWar, 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1