Thursday, November 11, 2010

Does anyone know how to program the addition of binary digits in VB 6?

I need to make a program in VB 6 that adds the binary digits of two text-Boxes. I made the conversion of decimal to binary, but don't know how to add the binaries now. I mean something like this: 01010010 + 0110101 = ?

I can do this by hand, but I need to program it! please help! 10pts.Thx!Does anyone know how to program the addition of binary digits in VB 6?
Basically you have to process both as strings.



If they are not the same length then you have to pad, on the left, the shorter string with 0's.



Once that is done then you move from the end to the start of the string looking at each pair of characters.



if str1[n] = '1' and str2[n] = '1' then

result[n] = '0'

if carry then result[n] = '1'

carry = true

else if (str1[n] = '1' and str2[n] = '0') or (str1[n] = '0' and str2[n] = '1') then

result[n] = '1'

if carry then

result[n] = '0'

carry = true

else

carry = false

end if

else

if carry then result[n] = '1' else result[n] = '0'

carry = false

end ifDoes anyone know how to program the addition of binary digits in VB 6?
Why do you have to add them together in binary? Why don't you just create a temporary variable for the decimal values, add them together and convert the sum back into binary?



I don't know if there is a built in function to do math with binary numbers; you could write one of course but that is dirty code, a waste of time. Why do it?



Unless you're in a programming class and they're making you do lame busy work; in which case that would be cheating helping you create the function.

No comments:

Post a Comment