i mange to know how it works |
I'm glad you did, because I certainly don't. I really don't understand why you apply the dec2bin function. If nothing else, the name suggests that it is intended for converting numbers from the decimal to binary in one sense or another. And I don't see how that fits with your problem. I have hard time understanding dec2bin as it is.
Let me explain how to extract the decimal digits out of some variable that stores a number.
Positional numerals are those written in the form:
d_n...d_2d_1d_0, where d_0...d_n are the digits, as in 1234 (one thousand two hundred and thirty four). Here, the digits are d_0 = 4, d_1 = 3, d_2 = 2, d_3 = 1. In this case, even though I wasn't explicit, the radix was 10. This means that there are 10 possible digits: 0..9.
Each digit from a numeral corresponds to a quantity. If you know the quantities that correspond to the digits in some positional numeral, and if you know the radix, there is a way to compute (or at least formulate) the quantity that corresponds to the entire numeral.
People have learned to associate decimal numerals with quantities, from memory and from intuition. That is why, to most, when you say numeral or quantity, it is difficult to distinguish between the two ideas. But in computing, the numeral is what denotes a quantity. The numeral is just one way to express the quantity. Like, you can use roman numerals too, and the latter are even not positional. That is, they don't abide the following formula.
If you have the numeral
d_n...d_2d_1d_0, and the radix (also called base) was b, then the formula for the corresponding quantity is (you can check in wikipedia):
Number = d_n * (b ^ n) + ... + d_2 * (b ^ 2) + d_1 * b + d_0 = sum { d_i * (b ^ i) }, i = 0..n
Here I use the caret (^) to denote exponentiation. In our case the radix is 10, but that is not particularly interesting.
What you need though, is the inverse. Given the quantity
Number, to find the quantities for the digits that would be part of its positional numeral. Again, you have to know the radix (10 in this case).
For this purpose, you need two operations. One that shifts the quantity to the right, and one that extracts d_0 every time (called the least significant digit).
Shifting right means that you take the original quantity that has some representation
d_n...d_2d_1d_0 and produce another quantity that has representation
d_n...d_2d_1. That is, you produce the quantity that would result if you stripped the least significant digit from the corresponding numeral. This is easy to do, because:
Original = d_n * (b ^ n) + ... + d_2 * (b ^ 2) + d_1 * b + d_0
Shifted = d_n * (b ^ (n - 1)) + ... + d_2 * (b ^ (2 - 1)) + d_1
The last formula you should check. Anyways, this means that
Shifted =
Original / b. It is important that the last division is integer division. Or in terms of ordinary division:
Shifted = floor(
Original / b).
Taking the last digit is also easy. You have that:
Original % b
= (d_n * (b ^ n)) % b + ... + (d_2 * (b ^ 2)) % b + (d_1 * b) % b + d_0 % b
= 0 + ... + 0 + 0 + d_0 = d_0
Here % is the remainder operation, as in C++. So, you have
Original % b = d_0. You should note that this works partly because the last digit d_0 is quantity smaller than the base. For example, in base 10, the digits 0..9 correspond to quantities smaller than 10.
So, having those two operations, you can consecutively extract the least significant digit, then shift the quantity right, then extract the next to least significant digit, etc. In radix 10, this means to first take % 10, which gives you d_0, then to divide by 10, which removes d_0 from the quantity and shifts the numeral to the right. You do this, until there is nothing left to shift right, i.e.
1 2 3 4 5 6
|
while (n)
{
d = n % 10;
n = n / 10;
//do something with d
}
|
In your case, it is preferable to simultaneously extract the least significant digits from both numbers. Then add them, see if there is a carry. Increment the carries counter if there is, and also remember if you had a carry or not. In the next step, again add the digits, but also, if you had carry from the last step, add 1. Now, this is elementary school arithmetic rules, so I am sure I don't have to remind you. You can terminate doing this when either quantity had been shifted right to 0, i.e. there is nothing left to extract.
Again, google for details. I even think that you could find this information on your own. But seeing what you came up with, I thought you need some direction.
Regards