2
\$\begingroup\$

It starts by taking two user input from the console and putting them into strings. I then pad them to get them to the same length. I get the length of the num1 and assign it to the integer len. I then start a for loop from 1 to the value of len.

I get the last charterer in num1 and num2 and assign then to char1 and char2. I then add pass which is a integer, num1 and num2 to get the total of a colum like if you were doing normal addition by hand. I do a bitwise AND to get check if the number would have anything in the units column. If it would I add on to the start of my total. Else I add a zero. I then do a right shift on the column_total and assign it to pass. This is to pass on any values which need to go into the next column.

In the next bit I have a while loop which would run until it clears out pass. I assign pass into column_total. I have the same logic to before with the if and else but instead of shifting column_total we now shift pass.

I then just write the total to console.

Module Module1
 
    Sub Main()
        Dim pass, column_total, len As Integer
        Dim total, char1, char2 As String
 
        Console.Write("Please enter your first binary number")
        Dim num1 As String = Console.ReadLine()
 
        Console.Write("Please enter your second binary number")
        Dim num2 As String = Console.ReadLine()
 
        If num1 > num2 Then
            num2 = num2.PadLeft(num1.Length() - num2.Length + 1, "0"c)
        Else
            num1 = num1.PadLeft(num2.Length() - num1.Length + 1, "0"c)
        End If
 
        len = num1.Length()
 
        For i = 1 To num1.Length()
            char1 = num1.Chars(len - i)
            char2 = num2.Chars(len - i)
            column_total = pass + char1 + char2
 
            If column_total And &H1 Then
                total = "1" + total
            Else
                total = "0" + total
            End If
            pass = column_total >> 1
            Console.WriteLine(total)
 
        Next
 
        While pass <> 0
            column_total = pass
            If column_total And &H1 Then
                total = "1" + total
            Else
                total = "0" + total
            End If
            pass >>= 1
        End While
 
        Console.WriteLine("The answer is: " & total)
        Console.ReadLine()
 
    End Sub
 
End Module
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.