Binary add

  • (2 Pages)
  • +
  • 1
  • 2

27 Replies - 1591 Views - Last Post: 08 June 2013 - 10:53 PM

#16 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7755
  • View blog
  • Posts: 13,114
  • Joined: 19-March 11

Re: Binary add

Posted 08 June 2013 - 12:40 AM

while mynum is not zero
  multiply binary by ten, add on the least significant digit of the number to be converter, and reduce the number to be converted by half. 



Notice that this is going to give you a deceptive number - the machine will think of this as being a decimal number, so that's not great. You might want to think about assembling this as a String, not as a number.


But that's not your real problem here, of course. Remember that you're peeling number off the least-significant side, and reducing from the least-significant side. That means that after each round, the numbers you're dealing with are more significant - not less.
Was This Post Helpful? 0
  • +
  • -

#17 TwoOfDiamonds  Icon User is offline

  • D.I.C Regular

Reputation: 54
  • View blog
  • Posts: 272
  • Joined: 27-July 12

Re: Binary add

Posted 08 June 2013 - 12:45 AM

Yes, I know, that's why it outputs reversed, I edited afterwards :D ... is that ok now ? And I used integers so I will be able to do this easily in C++ so I would see the result quickly without gettings strings involved.
Was This Post Helpful? 0
  • +
  • -

#18 TwoOfDiamonds  Icon User is offline

  • D.I.C Regular

Reputation: 54
  • View blog
  • Posts: 272
  • Joined: 27-July 12

Re: Binary add

Posted 08 June 2013 - 12:50 AM

#include <iostream>
#include <string>

using namespace std;

int main(int argc, char *argv[])
{
	int decimal;
	char ch[2];
	cin >> decimal;
	string binary;

	while (decimal != 0)
	{
		itoa (decimal%2, ch, 10);
		binary.insert(binary.begin(), ch[0]);
		decimal = decimal / 2;
	}

	cout << binary <<endl ;

	system("pause");

	return 0;
}


This outputs it right, but what I was asking is if the algorithm is the one you were talking about ^_^/> or I didn't get it right ?

EDIT: changed a to decimal

This post has been edited by TwoOfDiamonds: 08 June 2013 - 01:23 AM

Was This Post Helpful? 2
  • +
  • -

#19 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7755
  • View blog
  • Posts: 13,114
  • Joined: 19-March 11

Re: Binary add

Posted 08 June 2013 - 06:34 AM

Looks good to mw!
Was This Post Helpful? 0
  • +
  • -

#20 TwoOfDiamonds  Icon User is offline

  • D.I.C Regular

Reputation: 54
  • View blog
  • Posts: 272
  • Joined: 27-July 12

Re: Binary add

Posted 08 June 2013 - 06:38 AM

Thank you a lot sir !
Was This Post Helpful? 0
  • +
  • -

#21 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7755
  • View blog
  • Posts: 13,114
  • Joined: 19-March 11

Re: Binary add

Posted 08 June 2013 - 07:05 AM

No problem - glad I could help!
Was This Post Helpful? 0
  • +
  • -

#22 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5832
  • View blog
  • Posts: 12,686
  • Joined: 16-October 07

Re: Binary add

Posted 08 June 2013 - 07:27 AM

Looks good, but you're making that harder on yourself than it needs to be. You don't need itoa because you know exactly what value you should have in either case.

Perhaps:
while (decimal != 0) {
	char ch = '0';
	if (decimal%2==1) { ch='1'; }
	binary.insert(binary.begin(), ch);
	decimal /= 2;
}



Or, more tersely:
while (decimal != 0) {
	binary.insert(binary.begin(), ((decimal%2)?'1':'0'));
	decimal /= 2;
}


Was This Post Helpful? 0
  • +
  • -

#23 TwoOfDiamonds  Icon User is offline

  • D.I.C Regular

Reputation: 54
  • View blog
  • Posts: 272
  • Joined: 27-July 12

Re: Binary add

Posted 08 June 2013 - 07:36 AM

I thought about the first solution too :-? but sincerely, the second one seems much cleaner and by cleaner I mean no extra functions or variables.
Was This Post Helpful? 0
  • +
  • -

#24 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7755
  • View blog
  • Posts: 13,114
  • Joined: 19-March 11

Re: Binary add

Posted 08 June 2013 - 11:05 AM

View Postbaavgai, on 08 June 2013 - 09:27 AM, said:

Looks good, but you're making that harder on yourself than it needs to be. You don't need itoa because you know exactly what value you should have in either case.


True, but this cuts out the possibility of generalizing to arbitrary bases. (which seems to me the whole point of the exercise)

It's very easy to see how you change TwoOfDiamonds' version to make a function which converts from a string in base b into decimal - and from there, a generic base conversion routine. Can't really do that when you tie yourself to binary this way.
Was This Post Helpful? 0
  • +
  • -

#25 TwoOfDiamonds  Icon User is offline

  • D.I.C Regular

Reputation: 54
  • View blog
  • Posts: 272
  • Joined: 27-July 12

Re: Binary add

Posted 08 June 2013 - 01:20 PM

Jon, could you pretty please explain to me what you were saying in the previous post ? I didn't really understood. :(
Was This Post Helpful? 0
  • +
  • -

#26 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5832
  • View blog
  • Posts: 12,686
  • Joined: 16-October 07

Re: Binary add

Posted 08 June 2013 - 03:39 PM

View Postjon.kiparsky, on 08 June 2013 - 02:05 PM, said:

this cuts out the possibility of generalizing to arbitrary bases.


Fair enough, if that's the goal. However, itoa would still not be your friend here. You'd want a list of base values, not something tied to a base 10 assumption.

e.g.
string toBase(int n, int baseSize, char *symbols);

string toHex(int n) { return toBase(n, 16, "0123456789ABCDEF"); }


Was This Post Helpful? 0
  • +
  • -

#27 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7755
  • View blog
  • Posts: 13,114
  • Joined: 19-March 11

Re: Binary add

Posted 08 June 2013 - 04:23 PM

View PostTwoOfDiamonds, on 08 June 2013 - 03:20 PM, said:

Jon, could you pretty please explain to me what you were saying in the previous post ? I didn't really understood. :(/>



Just saying that all conversions from the internal representation (which, at the level you access it, is pretty much a pure quantity, if you think about it) to some base notation (base 2, base 3, base 64, you name it) are performed the same way: you peel off the least significant digit, which depends on your chosen base and not on the pure quantity you're dealing with, and you paste it into your string representation. Using binary logic to smooth out the binary case makes it impossible to generalize this out to bases other than 2, so it's a step in the wrong direction, unless you're interested in premature optimizations for their own sake. (as we know from Knuth, all optimizations are premature unless proved otherwise)

It's relatively easy to represent bases up to base 36, using the digits plus the 26 alphabetic characters. Go ahead and write a function that takes a value and a base, and displays the value in the chosen base. Then, if you like, write something that takes a string representing a number in base b and converts it to an int. And for giggles, wire the two together to convert hex to base 19 or something.
Was This Post Helpful? 1
  • +
  • -

#28 TwoOfDiamonds  Icon User is offline

  • D.I.C Regular

Reputation: 54
  • View blog
  • Posts: 272
  • Joined: 27-July 12

Re: Binary add

Posted 08 June 2013 - 10:53 PM

I got it now, thank you ! >.< Hope I don't give the impression that I'm stupid or something like that by asking every little thing >.<
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2