Posted on by Kalkicode
Code Bit Logic

BCD addition of two numbers in vb.net

Vb program for BCD addition of two numbers. Here more information.

' Include namespace system
Imports System 
'   Vb.net program for
'   BCD addition of two decimal numbers
public Class Addition
    '  Get valid value
    Public Function  getValue(ByVal num As Integer) As Char
        if (num >= 0 AndAlso num <= 9) Then
            Return  Chr((num + Int("0"c)))
        Else
            Return  Chr((num - 10 + Int("A"c)))
        End IF
    End Function
    '  Get 4 bit binary value of a digit
    Public Function  binaryValue(ByVal num As Integer, 
                                 ByVal extra As Boolean) As String
        Dim result As String = ""
        Dim n As Integer = num
        while (n > 0)
            result = (Me.getValue(n Mod 2)).ToString() + result
            n /= 2
        End While
        if (extra = True) Then
            '  When need to include extra bit
            n = result.Length
            while (n < 4)
                result = "0" + result
                n += 1
            End While
        End If
        Return  result
    End Function
    '  Find BCD addition of given x and y
    Public Sub bcdAddition(ByVal x As Integer, 
                           ByVal y As Integer)
        Dim sum As Integer = x + y
        if (sum < 0) Then
            Return
        End If
        Dim ans As String = ""
        while (sum > 9)
            ans = Me.binaryValue(sum Mod 10, True) + ans
            sum = (sum / 10)
        End While
        '  Add a first bit
        ans = Me.binaryValue(sum Mod 10, False) + ans
        '  Display calculated result
        Console.Write( vbLf &" (" + x.ToString() + 
                      " + " + y.ToString() + ")")
        Console.Write(" : " + ans)
    End Sub
    Public Shared Sub Main(ByVal args As String())
        Dim task As Addition = New Addition()
        '  Test A
        '      x = 12  
        '      y = 6   
        '  ======================
        '  x + y = 18 => (1 = 1) (8 = 1000)  
        '       (11000)   
                task.bcdAddition(12, 6)
        '  Test B
        '      x = 7  
        '      y = 18  
        '  ======================
        '         25 => 
        '   (2 = (10) 5 (0101))    
        '        (100101)  
        task.bcdAddition(7, 18)
        '  Test C
        '      x = 45   
        '      y = 32   
        '  ======================
        '          77
        '  (7 = 111) (7 = 0111)
        '      (1110111)  
        task.bcdAddition(45, 32)
        '  Test D
        '      x = 111   
        '      y = 132   
        '  ======================
        '          243
        '  (2 = 10) (4 = 0100) (3 = 0011)
        '     (1001000011)  
        task.bcdAddition(111, 132)
    End Sub
End Class

Output

 (12 + 6) : 101000
 (7 + 18) : 100101
 (45 + 32) : 10001001
 (111 + 132) : 1001000101

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