Does anyone know how to program the sum of two binary numbers? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Does anyone know how to program the sum of two binary numbers?

in c language

13th Jan 2021, 8:18 PM
Ivette Flores
Ivette Flores - avatar
4 Answers
+ 3
One way: shift or set the input binary bit values into integer variables, add the integer variables, then translate the result into binary for display. Another way: bit by bit perform xor (^) to get the output result of binary addition and use bitwise and (&) to get the carry value to add to the next column. If there is a carry value you must add it to the next column and the next... until the carry is zero. Then go back to where the carry was first generated and add the next column.
13th Jan 2021, 9:38 PM
Brian
Brian - avatar
+ 1
Martin Taylor via DM I was informed that this is a school assignment, and doing it the easy way, as you and I recommended, is disallowed. I wrote a solution to this and will try to mentor the OP toward her own assignment completion.
14th Jan 2021, 11:40 AM
Brian
Brian - avatar
+ 1
Martin Taylor unfortunately this is how education is in Mexico
14th Jan 2021, 3:52 PM
Ivette Flores
Ivette Flores - avatar
0
Create a function that takes the binary value (as a string), make a for loop that iterates over the string in reverse. Create a variable to sum up the values. You need to declare a second variable in the for loop and use that variable with the pow function from the math.h and do e.g sum+=pow(2, i) and return the sum....then..just call the function twice e.g getInt("101") + getInt("1001"); Voila.
13th Jan 2021, 10:34 PM
rodwynnejones
rodwynnejones - avatar