Thursday, September 30, 2010

Solution of the Riddle

In a previous post I presented a little programming riddle. Now the solution:
The "gadget" I was looking for is a full adder.
That is a digital circuit, which can add 3 binary numbers. So the circuit has 3 inputs for the 3 numbers to be added and 2 outputs, s and c.
The output s (sum) outputs the digit with low value, the ouput c (carry) the one with high value.
You can find more detailed information on wikipedia.
In the code class from the riddle the calculation is started by the method ABC(). This expects 3 strings as parameters, which expect the binary numbers to be added.
The letter "x" represents digit 0, "y" digit 1. First now DEF() is called, in which is checked in multiple ways, whether really only "x" and "y" are given as inputs.
If a not valid input is given, the program returns "d", representing error. In ABC() otherwise the sum of the 3 numbers is calculated and the result returned as a string. The left digit of the result string is the lower one of the sum (s), the right one the high (c).
In the result now an "a" stands for a 0, a "b" for a 1.

No comments:

Post a Comment