Add two numbers using bitwise operators in vb.net

Vb program for Add two numbers using bitwise operators. Here problem description and explanation.

' Include namespace system
Imports System 
'  Vb.net program for
'  Add two numbers using of bitwise operators
public Class NumberAddition
    '  Add two numbers
    Public Sub addition(ByVal a As Integer, ByVal b As Integer)
        '  Display given numbers
        Console.Write( vbLf &" [({0}) + ({1})] ",a,b)
        '  Auxiliary variable which are store the carry bit result
        Dim carry As Integer = 0
        '  Executing loop, until b value is not zero
        while (b <> 0)
            '  Get carry bits of a & b
            carry = a And b
            '  Sum of bits of a ^ b
            a = a Xor b
            '  Shift the carry bit by one bit in left side
            b = carry << 1
        End While
        '  Display add result
        Console.Write(" : {0}", a)
    End Sub
    Public Shared Sub Main(ByVal args As String())
        Dim task As NumberAddition = New NumberAddition()
        '  Test Case
        task.addition(8, -2)
        task.addition(5, 3)
        task.addition(-3, -5)
    End Sub
End Class

Output

 [(8) + (-2)]  : 6
 [(5) + (3)]  : 8
 [(-3) + (-5)]  : -8



Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2022, kalkicode.com, All rights reserved