A binary number is a base-2 number composed of the binary digits (bits) 0 and 1. For

example, the binary numbers 1, 10, 11, 111 are of decimal values 1, 2, 3, 7,

respectively. Addition of two binary digits follow the rules of 0+0=0, 0+1=1, 1+0=1 and

1+1=10. The sum of two binary numbers can then be obtained in the same way as

we add two decimal numbers, i.e., starting from the least significant place, we add the

corresponding digits of the two numbers and propagate any carry digit to the next,

more significant place. The following two figures illustrate the addition for decimal

numbers and for binary numbers. Note that the carry bit for binary addition can only In 1.cpp , write a C++ program that reads two strings, each representing a binary

number, supplied by the user. The input strings are therefore binary strings containing

a sequence of 0’s and 1’s. Your program should output the sum of the two binary

numbers. You may assume that the input numbers, if nonzero, always start with a 1 .

This is the same for the output sum, i.e., a nonzero sum should always start with a

1 . You should NOT make any assumption to the maximum length of the input

binary strings (i.e., the input strings can be as long as what a string object in C++

can be.)

Test cases

1_1

10101010

11001100

101110110

1_2

11111

1

100000

1_3

0

0

0

1_4

10001

11

10100

1_5

110001110001001010100111011101100101001011010111

10111000100110001010001000110110010111000001111

1001000110101111011111000100100011000000011100110

I have no idea how can i do it!Please give me a hand.

example, the binary numbers 1, 10, 11, 111 are of decimal values 1, 2, 3, 7,

respectively. Addition of two binary digits follow the rules of 0+0=0, 0+1=1, 1+0=1 and

1+1=10. The sum of two binary numbers can then be obtained in the same way as

we add two decimal numbers, i.e., starting from the least significant place, we add the

corresponding digits of the two numbers and propagate any carry digit to the next,

more significant place. The following two figures illustrate the addition for decimal

numbers and for binary numbers. Note that the carry bit for binary addition can only In 1.cpp , write a C++ program that reads two strings, each representing a binary

number, supplied by the user. The input strings are therefore binary strings containing

a sequence of 0’s and 1’s. Your program should output the sum of the two binary

numbers. You may assume that the input numbers, if nonzero, always start with a 1 .

This is the same for the output sum, i.e., a nonzero sum should always start with a

1 . You should NOT make any assumption to the maximum length of the input

binary strings (i.e., the input strings can be as long as what a string object in C++

can be.)

Test cases

1_1

10101010

11001100

101110110

1_2

11111

1

100000

1_3

0

0

0

1_4

10001

11

10100

1_5

110001110001001010100111011101100101001011010111

10111000100110001010001000110110010111000001111

1001000110101111011111000100100011000000011100110

I have no idea how can i do it!Please give me a hand.

Consider:

1234

12

The answer is 1246, not 2434 as you are doing above. In decimal, you add units, tens, hundreds, ... together and not mix them, and have to use carry to cross these boundaries. The same goes for binary.

So, 4 is:

10001 + 00011, not 10001 + 11000.

1234

12

The answer is 1246, not 2434 as you are doing above. In decimal, you add units, tens, hundreds, ... together and not mix them, and have to use carry to cross these boundaries. The same goes for binary.

So, 4 is:

10001 + 00011, not 10001 + 11000.

You do it the same way as you do decimal.

I've been trying to post an example, but can't get the formatting right.

I've been trying to post an example, but can't get the formatting right.

Last edited on

Registered users can post here. Sign in or register to post.