|
|
|
|
|
Simple VB.net Code for Binary Addition |
|
|
|
|
|
Shown below is a simple VB.net code
for Binary Addition, along
with a screenshot of the form used with it, for reference by students
learning to program in VB.net in the context of electronics and computer
engineering. Although the code doesn't show the mechanics of binary
addition, it shows how VB.net can be used to manipulate numbers from
different base systems.
|

Public
Class frmMain Inherits System.Windows.Forms.Form
Function
dec2baseN(ByVal value, ByVal outBase) As
String
'Converts
base 10 to any base
Dim q
'quotient
Dim r
'remainder
Dim m 'denominator
Dim y
'converted value
m = outBase
q = value
Do
r = q
Mod
m
q = Int(q / m)
If r >= 10
Then
r = Chr(65 + (r - 10))
End
If
y = y &
CStr(r)
Loop Until
q = 0
dec2baseN = StrReverse(y)
End Function
Function
baseN2dec(ByVal value, ByVal inBase) As
String
'Converts
any base to base 10
Dim
strValue, i, x, y
strValue = StrReverse( CStr(UCase(value)))
For i = 0 To
Len(strValue) - 1
x = Mid(strValue, i + 1, 1)
If Not IsNumeric(x)
Then
y = y + ((Asc(x) - 65) + 10) * (inBase ^ i)
Else
y = y + ((inBase ^ i) *
CInt(x))
End
If
Next
baseN2dec = y
End Function
Private Sub
cmdCompute_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles
cmdCompute.Click
txtOut.Text = dec2baseN((Val(baseN2dec(txtB1.Text, 2))
+Val(baseN2dec(txtB2.Text, 2))), 2)
End
Sub
End
Class |
|
|
|
|
|
|
|