binary system

how can i write sum of two numbers in binary system in c++?i really need help
 
cout <<"sum of two numbers in binary system";


No seriously, what exactly is the question?
nooo,i dont know how can i write this "sum" in c++
That is not a very accurate description of what you are trying to do. If you don't know what you're trying to do yourself, I understand that you can't do it, but if you do know you better tell us.
actually i have no idea about binary system,that's why
You don't understand the math? Just say so. Basically, it works pretty much the same as in the decimal system
1)
10001
+01110
=11111
2)
00011
+00011
=00110

Now the steps:

10001
+01110

You start from the right, and add the 2 rightmost digits together - and then keep going left
1+0 = 1 so you write a 1 there
0+1 = 1
0+1 = 1
0+1 = 1
1+0 = 1

so you get 11111

now the other, basically it's the same thing, just now you gotta introduce carries:

1+1 = 0 carry 1
1+1 = 0 + 1 (carry) = 1 carry 1
0+0 = 0 + 1 (carry) = 1

If you didn't understand that, just compare it to addition in the decimal system:

19
+29

9+9 = 8 carry 1
1+2 = 3 + 1 (carry) = 4

so the result is 48.
Last edited on
perhaps what you want to do is like this:

 
fstream my_file ("some_file", ios::binary);


so you can write it in binary file. but the rest of it i don't know -_-!
chips, that's definitely NOT what he wants to do. ios::binary just means that the stream is a stream of bytes rather than a text stream (which also is a byte stream of course, but formatting and stuff like that is applied here).
thank you,very much hanst99,could you tell how to write a+b like that in c++,i tried to do this problem http://www.e-olimp.com/en/problems/1001
This is your task, not mine. The algorithm for doing that is above, just implement that in C++.

PS: Does the task actually require that you to do the addition in the binary system? Cause if not, you could just read 2 strings, parse them so you get two ints, add those and then output the result as a binary number.
ok thank you very much i really appreciate it.
oh sorry :D
Topic archived. No new replies allowed.