# Adding Binary Numbers

Page 1 of 1

## 6 Replies - 27276 Views - Last Post: 14 June 2010 - 07:47 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=177593&amp;s=5b44fb1c9d3e8ba27536a8c5087dffc0&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 marcelomg

• New D.I.C Head

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

• Can grep dead trees!

Reputation: 2255
• 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.

### #3 buffalobill

Reputation: 21
• 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?

### #4 Aphex19

• Born again Pastafarian.

Reputation: 619
• 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.

### #5 CTphpnwb

• D.I.C Lover

Reputation: 3833
• 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!

### #6 baavgai

• Dreaming Coder

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

## Re: Adding Binary Numbers

Posted 14 June 2010 - 07:20 AM

CTphpnwb, 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.

### #7 Frinavale

Reputation: 205
• 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