Adding Binary Numbers

C++ function to add binary numbers.

Page 1 of 1

6 Replies - 27276 Views - Last Post: 14 June 2010 - 07:47 AM Rate Topic: -----

#1 marcelomg   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 10-January 09

Adding Binary Numbers

Posted 13 June 2010 - 07:23 PM

Hello,

Can anyone help me with solve a problem that I have to add two binary numbers.
I have to get two numbers from user and compute the sum in binary.

Folks, I'm not asking for the code!
I need to know how to get this in C++. Is there a special library (function) to convert decimals to binary? Or do I need to convert the numbers myself?
I've been looking for tutorials on this but can't find anything.
Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Adding Binary Numbers

#2 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Adding Binary Numbers

Posted 13 June 2010 - 07:52 PM

Quote

I've been looking for tutorials on this but can't find anything.
Why would you do that?

I don't know the details of your assignment but computers can ONLY add binary numbers so really this should be pretty easy.

int sum = a + b; // adding two binary numbers

ok... so I realize you are probably supposed to read a number from the user like "101011101101" and add it to "1011" which is not really all that hard either. If you look at our snippets there are quite a few versions.

As for writing a number to the console in binary form that is every so slightly more tricky but not too much trouble.


Look this is a really basic example that students get assigned over and over and over and the countless other students mannaged to get it done you can to. Stop searching the internet for a way to cheat and just sit down and think it though. It is pretty easy. If you MUST search the internet, try looking at the 100's of times this question has been asked in this forum alone.
Was This Post Helpful? 0
  • +
  • -

#3 buffalobill   User is offline

  • D.I.C Head

Reputation: 21
  • View blog
  • Posts: 188
  • Joined: 08-July 08

Re: Adding Binary Numbers

Posted 13 June 2010 - 09:32 PM

Do you mean that you get two numbers from a user in decimal format, sum them, then convert that sum to binary format or do you mean get two numbers from a user in binary format, sum the two binary numbers and present that result in binary or decimal format?
Was This Post Helpful? 0
  • +
  • -

#4 Aphex19   User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 619
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: Adding Binary Numbers

Posted 14 June 2010 - 07:05 AM

If you had two binary string, for example.

"01101001"
"11010011"

Start by converting them both to decimal, using this algorithm. (It's not that difficult, i managed to convert any binary string to decimal in about 6 lined of code), then simply add them together.
Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3833
  • View blog
  • Posts: 13,972
  • Joined: 08-August 08

Re: Adding Binary Numbers

Posted 14 June 2010 - 07:18 AM

Another way to approach this is to do the math manually first:

01101001 + 11010011

1 + 1 = 0 carry a 1, so we get 0 for the right most digit, followed by 1+0 +1, which is 0 carry a 1, making the third digit is 1:
xxxxx100
Continue with the rest the same way. When you've got that down, code it!
Was This Post Helpful? 1
  • +
  • -

#6 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7507
  • View blog
  • Posts: 15,558
  • Joined: 16-October 07

Re: Adding Binary Numbers

Posted 14 June 2010 - 07:20 AM

View PostCTphpnwb, on 14 June 2010 - 08:18 AM, said:

Another way to approach this is to do the math manually first:


I expect this is what your instructor is looking for. Also, using the more manual approach, you can simply start with a string.
Was This Post Helpful? 0
  • +
  • -

#7 Frinavale   User is offline

  • D.I.C Addict
  • member icon

Reputation: 205
  • View blog
  • Posts: 776
  • Joined: 03-June 10

Re: Adding Binary Numbers

Posted 14 June 2010 - 07:47 AM

If I were you I'd start by researching algorithms for binary addition.
Since I just took a course on data organization and CPU level processing I'll help you by pointing you in the direction of Booth's binary addition algorithm. I had to memorize it for my final exam and write each iteration of the algorithm manually for my final exam (I think some teachers just like to torture students).

Anyways, once you understand the algorithm it's as simple as implement it.
Since you're not working at bit-wise operator level you'll probably have to use arrays...

I should stop thinking about ways to solve your assignment and let you give it a go :)

-Frinny
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1