Binary Addition of Strings UDF issue

Juggler_IN

Active Member
Joined
Nov 19, 2014
Messages
349
Office Version
  1. 2003 or older
Platform
  1. Windows
I am facing an issue with a C++ code conversion to VBA. If you provide two binary strings the function outputs the addition of two strings in binary format.

For example, if a = 11 == "00001011" and b = 36 == 00100100, then a+b = 47 == 00101111. The function also outputs 00101111.

If we provide string input, say within 1,25 using RANDBETWEEN using DEC2BIN then this function in most instances gives the desired output except for few exceptions such as, if a = 11 == 00001011 and b = 35 == 00100011, then a+b = 46 and the output SHOULD be 00101110 but the function is EVALUATING to 00110010 == 50.

I am unable to decode the issue. Any thoughts?

My UDF is:
VBA Code:
Private Function solve( _
        ByVal a As String, _
        ByVal b As String) As String

    Dim na%, nb%
    Dim carry%
    Dim i%, j%, k%
    Dim addA%, addB%, sum%
    Dim u, v, ret$

    ret = ""
    na = VBA.Len(a)
    nb = VBA.Len(b)
    i = (na - 1)
    j = (nb - 1)
    carry = 0

    ReDim u(0 To i)
    ReDim v(0 To j)

    For k = 0 To na - 1
        u(k) = VBA.Mid(a, k + 1, 1)
        Debug.Print u(k);
    Next
    Debug.Print

    For k = 0 To nb - 1
        v(k) = VBA.Mid(b, k + 1, 1)
        Debug.Print v(k);
    Next
    Debug.Print

    Do While ((i >= 0) Or (j >= 0))

        If (i >= 0) Then
            addA = (u(i) - VBA.ChrW(48))
        Else
            addA = 0
        End If

        If (j >= 0) Then
            addB = (v(j) - VBA.ChrW(48))
        Else
            addB = 0
        End If

        sum = addA + addB + carry
        carry = sum / 2&
        sum = sum Mod 2&
        ret = ret & "" & sum
        i = (i - 1)
        j = (j - 1)

    Loop

    If carry <> 0 Then
        ret = ret & "" & carry
    End If

    solve = VBA.StrReverse(ret)
    Exit Function

End Function

C++ source code is -- (The link below provides a online execution of this code with different values.)
Code:
class Solution {
   public:
   string solve(string a, string b){
      string ret = "";
      int na = a.size();
      int nb = b.size();
      int i = na - 1;
      int j = nb - 1;
      int carry = 0;
      while(i >= 0 || j >= 0){
         int addA = i >= 0 ? a[i] - '0' : 0;
         int addB = j >= 0 ? b[j] - '0' : 0;
         int sum = addA + addB + carry;
         carry = sum / 2;
         sum %= 2;
         ret += to_string(sum);
         i--;
         j--;
      }
      if(carry)
         ret += to_string(carry); reverse(ret.begin(), ret.end());
         return ret;
   }
};
main(){
   string a = "10110", b = "10010"; Solution ob;
   cout << ob.solve(a, b);
}

The C++ source code is atBinary Addition
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
What about just using Excel's built-in functions directly...
VBA Code:
Function BinaryAdd(BinStr1 As String, BinStr2 As String) As String
  With Application
    BinaryAdd = Format(.Dec2Bin(.Bin2Dec(BinStr1) + .Bin2Dec(BinStr2)), "00000000")
  End With
End Function
 
Upvote 0
@Rick Rothstein ; Thanks, I do have this direct version but am interested in the execution as per the c++ code. I am unable to debug the issue in VBA version. Any thoughts?
 
Upvote 0
How about this then...
VBA Code:
Function BinaryAdd(BinStr1 As String, BinStr2 As String) As String
  Dim X As Long, Temp As Long, Carry As Long
  BinStr1 = Format(BinStr1, "00000000")
  BinStr2 = Format(BinStr2, "00000000")
  BinaryAdd = "00000000"
  For X = 8 To 1 Step -1
    Temp = CLng(Mid(BinStr1, X, 1)) + CLng(Mid(BinStr2, X, 1)) + Carry
    If Temp > 1 Then
      Mid(BinaryAdd, X) = Temp Mod 2
      Carry = 1
    Else
      Mid(BinaryAdd, X) = Temp
      Carry = 0
    End If
  Next
End Function
 
Upvote 0
Try this
VBA Code:
Function AddBinary(BStr1 As String, BStr2 As String) As String
Dim K As String
Dim temp As Long
Dim T As Integer, TA As Integer, Mul As Integer

For T = 1 To 2
If T = 1 Then K = BStr1 Else K = BStr2
Mul = 1
    For TA = 1 To Len(K)
    temp = temp + Mul * Mid(K, Len(K) - TA + 1, 1)
    Mul = Mul * 2
    Next TA
Next T

K = ""
Do While temp > 1
K = temp Mod 2 & K
temp = Int(temp / 2)
Loop

AddBinary = Format(temp & K, "00000000")
End Function
 
Upvote 0

Forum statistics

Threads
1,215,254
Messages
6,123,893
Members
449,131
Latest member
leobueno

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top