0
Does anyone know how to program the sum of two binary numbers?
in c language
6 Réponses
+ 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.
+ 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.
+ 1
Martin Taylor unfortunately this is how education is in Mexico
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.