VB.NET中怎样实现位运算
TextBox1.Text = 7 And 10
我们提供的服务有:网站建设、成都网站建设、微信公众号开发、网站优化、网站认证、克州ssl等。为上1000家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的克州网站制作公司
TextBox1.Text = 7 Or 10
TextBox1.Text = 7 Xor 10
TextBox1.Text = Not 10
TextBox1.Text = 7 10
TextBox1.Text = 7 10
如这些,我在VB.Net中也找不到shl和shr运算符~
不过可以通过"自定义运算符"来写出我们想要的功能
VB.NET怎么位运算?
注意只能对整型执行按位运算。浮点值必须转换为整型后,才能执行按位运算。按位运算采用二进制(以 2 为基)形式计算两个整数值。它们比较对应位置上的位,然后基于比较的结果赋值。下面的示例演示了 And 运算符。复制Dim x As Integerx = 3 And 5 前面的示例将 x 的值设置为 1。发生这种情况的原因如下:这些值以二进制形式处理:二进制格式的 3 为 011二进制格式的 5 为 101And 运算符比较这些二进制表示方式,一次比较一个二进制位置(位)。如果给定位置的两个位都为 1,则将 1 放在结果中的该位置。如果任何一个位是 0,则将 0 放在结果中的该位置。在前面的示例中,按如下所示计算结果:011(二进制格式的 3)101(二进制格式的 5)001(二进制格式的计算结果)计算结果以十进制形式处理。值 001 是 1 的二进制表示形式,因此 x = 1。除了在任何一个比较位是 1 或两个比较位都是 1 的情况下将 1 赋予结果位以外,按位 Or 运算与此类似。Xor 在比较的位正好只有一个是 1(而不是两者都是 1)时将 1 赋给结果位。Not 采用单个操作数并反转所有位(包括符号位),然后将该值赋予结果。这意味着,对于有符号正数,Not 始终返回负值,而对于负数,Not 始终返回正值或零。AndAlso 和OrElse 运算符不支持按位运算。 追问: 好复杂啊...还是不会,还有整数怎么转换为二进制数? 回答: 为什么一定要用位运算呢,你那个乘法只能通过左移操作符“ ”来进行,而左移一次代表乘以2,要是乘以一个小数,就必须先化成2的n次方,不够再用加减法调整,很麻烦啊,没必要用位运算啊。。。 追问: 因为我要进行大量的运算,但速度要快,所有用位运算...我也不想啊.. 回答: 那就不该用VB 啊,c/c++在执行效率上是没话说的。 追问: 问题就是不会嘛... 回答: 那你上csdn上发帖效果应该不错 追问: CSDN发了,我发在高性能运算,没人... 回答: 耐心一些,或者你应该发到VB.NET论坛那里。。。 追问: 额..只能这样了... 提问者 的感言: 太复杂了..算了 2010-11-08
vb 十进制转换成 八进制 二进制原码 二进制补码 二进制反码 程序
VB.NET控制台程序:
Module module3
Sub Main()
Dim i As Integer
Dim num As Integer = -35
Dim R As Integer = 8
Dim arr(33) As Integer
Dim len As Integer
arr = TransToR(num, R)
len = arr(0)
Console.Write(num " 转换成 " R " 进制后:" vbTab)
If (arr(1) = 1) Then
Console.Write("-")
End If
For i = len + 1 To 2 Step -1
Console.Write(arr(i))
Next
Console.WriteLine()
arr = TransToSourceCode(num)
Console.Write(num " 转换成32位二进制源码后:" vbTab)
Console.Write(arr(1))
For i = 32 To 2 Step -1
Console.Write(arr(i))
Next
Console.WriteLine()
arr = TransToReverseCode(num)
Console.Write(num " 转换成32位二进制反码后:" vbTab)
Console.Write(arr(1))
For i = 32 To 2 Step -1
Console.Write(arr(i))
Next
Console.WriteLine()
arr = TransToCompensatoryCode(num)
Console.Write(num " 转换成32位二进制补码后:" vbTab)
Console.Write(arr(1))
For i = 32 To 2 Step -1
Console.Write(arr(i))
Next
Console.WriteLine()
End Sub
'将十进制数num转换为R(2=R=9)进制数,结果存在arr数组中
'arr(0)存储长度, arr(1)存储符号位:1为负,0为正
Function TransToR(ByVal num As Integer, ByVal R As Integer) As Integer()
Dim arr(33) As Integer
arr(0) = 0 '初始长度
'设置符号位
If (num 0) Then
arr(1) = 1
Else
arr(1) = 0
End If
num = Math.Abs(num)
While num 0
arr(0) = arr(0) + 1
arr(arr(0) + 1) = num Mod R
num \= R
End While
Return arr
End Function
'将十进制数num转换为二进制原码
Function TransToSourceCode(ByVal num As Integer) As Integer()
Dim arr(33) As Integer
Dim i As Integer
arr = TransToR(num, 2)
'高位补零
For i = arr(0) + 2 To 32
arr(i) = 0
Next
arr(0) = 32
Return arr
End Function
'将十进制数num转换为二进制反码
Function TransToReverseCode(ByVal num As Integer) As Integer()
Dim arr(33) As Integer
Dim i As Integer
arr = TransToSourceCode(num)
If num = 0 Then
Return arr
End If
'除符号位外各位取反
For i = 2 To 33
arr(i) = 1 - arr(i)
Next
Return arr
End Function
'将十进制数num转换为二进制补码
Function TransToCompensatoryCode(ByVal num As Integer) As Integer()
Dim arr(33) As Integer
Dim i As Integer
Dim c As Integer = 0 '进位
arr = TransToReverseCode(num)
If num = 0 Then
Return arr
End If
'末尾加1
i = 2
arr(i) += 1
Do While True
arr(i) = c + arr(i)
c = arr(i) \ 2
arr(i) = arr(i) Mod 2
i += 1
If c 1 Then
Exit Do
End If
Loop
Return arr
End Function
End Module
vb.net的问题,求回复
Not一个数值实际上是对这个数值进行“非”运算,即转换为2进制,0变为1,1变为0,你可以这样试:
For i = 1 To 10
s = i
debug.print not (i Mod 5 )
Next
这样可以看到not (i Mod 5 )这个表达式究竟等于多少。
按照你的想法,应该这样做结果才是对的:
If Not (i Mod 5 0) Then
VB.NET中的一个方法
用replace函数
Replace(expression, find, replacewith)
Replace函数语法有如下几部分:
expression 字符串表达式,包含要替换的子字符串。
find 要搜索到的子字符串。
replacewith 用来替换的子字符串。
以你的要求:
str=Replace(A,"A","a")
如果你要做一个学生姓名对应一个学生学号,你输了姓名,学号自动就显示出来了.
最好通过数据库实现.
select studentid from student where studentname=输入姓名
用VB.NET编的计算器程序!
Public Class SimpleCalculator
Inherits System.Windows.Forms.Form
#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
End Sub
'窗体重写处置以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
'注意:以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents Button3 As System.Windows.Forms.Button
Friend WithEvents Button4 As System.Windows.Forms.Button
Friend WithEvents Button5 As System.Windows.Forms.Button
Friend WithEvents Button6 As System.Windows.Forms.Button
Friend WithEvents Button7 As System.Windows.Forms.Button
Friend WithEvents Button8 As System.Windows.Forms.Button
Friend WithEvents Button9 As System.Windows.Forms.Button
Friend WithEvents Button10 As System.Windows.Forms.Button
Friend WithEvents Button11 As System.Windows.Forms.Button
Friend WithEvents Button12 As System.Windows.Forms.Button
Friend WithEvents Button13 As System.Windows.Forms.Button
Friend WithEvents Button14 As System.Windows.Forms.Button
Friend WithEvents Button15 As System.Windows.Forms.Button
Friend WithEvents Button16 As System.Windows.Forms.Button
System.Diagnostics.DebuggerStepThrough() Private Sub InitializeComponent()
Me.Label1 = New System.Windows.Forms.Label
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.Button1 = New System.Windows.Forms.Button
Me.Button2 = New System.Windows.Forms.Button
Me.Button3 = New System.Windows.Forms.Button
Me.Button4 = New System.Windows.Forms.Button
Me.Button5 = New System.Windows.Forms.Button
Me.Button6 = New System.Windows.Forms.Button
Me.Button7 = New System.Windows.Forms.Button
Me.Button8 = New System.Windows.Forms.Button
Me.Button9 = New System.Windows.Forms.Button
Me.Button10 = New System.Windows.Forms.Button
Me.Button11 = New System.Windows.Forms.Button
Me.Button12 = New System.Windows.Forms.Button
Me.Button13 = New System.Windows.Forms.Button
Me.Button14 = New System.Windows.Forms.Button
Me.Button15 = New System.Windows.Forms.Button
Me.Button16 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(16, 16)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(48, 16)
Me.Label1.TabIndex = 0
Me.Label1.Text = "结果:"
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(64, 8)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(120, 21)
Me.TextBox1.TabIndex = 1
Me.TextBox1.Text = ""
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(200, 8)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(56, 24)
Me.Button1.TabIndex = 2
Me.Button1.Text = "清空"
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(48, 56)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(24, 24)
Me.Button2.TabIndex = 3
Me.Button2.Text = "1"
'
'Button3
'
Me.Button3.Location = New System.Drawing.Point(88, 56)
Me.Button3.Name = "Button3"
Me.Button3.Size = New System.Drawing.Size(24, 24)
Me.Button3.TabIndex = 4
Me.Button3.Text = "2"
'
'Button4
'
Me.Button4.Location = New System.Drawing.Point(136, 56)
Me.Button4.Name = "Button4"
Me.Button4.Size = New System.Drawing.Size(24, 24)
Me.Button4.TabIndex = 5
Me.Button4.Text = "3"
'
'Button5
'
Me.Button5.Location = New System.Drawing.Point(48, 88)
Me.Button5.Name = "Button5"
Me.Button5.Size = New System.Drawing.Size(24, 24)
Me.Button5.TabIndex = 6
Me.Button5.Text = "4"
'
'Button6
'
Me.Button6.Location = New System.Drawing.Point(88, 88)
Me.Button6.Name = "Button6"
Me.Button6.Size = New System.Drawing.Size(24, 24)
Me.Button6.TabIndex = 7
Me.Button6.Text = "5"
'
'Button7
'
Me.Button7.Location = New System.Drawing.Point(136, 88)
Me.Button7.Name = "Button7"
Me.Button7.Size = New System.Drawing.Size(24, 24)
Me.Button7.TabIndex = 8
Me.Button7.Text = "6"
'
'Button8
'
Me.Button8.Location = New System.Drawing.Point(48, 120)
Me.Button8.Name = "Button8"
Me.Button8.Size = New System.Drawing.Size(24, 24)
Me.Button8.TabIndex = 9
Me.Button8.Text = "7"
'
'Button9
'
Me.Button9.Location = New System.Drawing.Point(88, 120)
Me.Button9.Name = "Button9"
Me.Button9.Size = New System.Drawing.Size(24, 24)
Me.Button9.TabIndex = 10
Me.Button9.Text = "8"
'
'Button10
'
Me.Button10.Location = New System.Drawing.Point(136, 120)
Me.Button10.Name = "Button10"
Me.Button10.Size = New System.Drawing.Size(24, 24)
Me.Button10.TabIndex = 11
Me.Button10.Text = "9"
'
'Button11
'
Me.Button11.Location = New System.Drawing.Point(48, 152)
Me.Button11.Name = "Button11"
Me.Button11.Size = New System.Drawing.Size(24, 24)
Me.Button11.TabIndex = 12
Me.Button11.Text = "0"
'
'Button12
'
Me.Button12.Location = New System.Drawing.Point(176, 56)
Me.Button12.Name = "Button12"
Me.Button12.Size = New System.Drawing.Size(24, 24)
Me.Button12.TabIndex = 13
Me.Button12.Text = "+"
'
'Button13
'
Me.Button13.Location = New System.Drawing.Point(176, 88)
Me.Button13.Name = "Button13"
Me.Button13.Size = New System.Drawing.Size(24, 24)
Me.Button13.TabIndex = 14
Me.Button13.Text = "-"
'
'Button14
'
Me.Button14.Location = New System.Drawing.Point(176, 120)
Me.Button14.Name = "Button14"
Me.Button14.Size = New System.Drawing.Size(24, 24)
Me.Button14.TabIndex = 15
Me.Button14.Text = "*"
'
'Button15
'
Me.Button15.Location = New System.Drawing.Point(176, 152)
Me.Button15.Name = "Button15"
Me.Button15.Size = New System.Drawing.Size(24, 24)
Me.Button15.TabIndex = 16
Me.Button15.Text = "/"
'
'Button16
'
Me.Button16.Location = New System.Drawing.Point(88, 152)
Me.Button16.Name = "Button16"
Me.Button16.Size = New System.Drawing.Size(72, 24)
Me.Button16.TabIndex = 17
Me.Button16.Text = "计算"
'
'SimpleCalculator
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(264, 190)
Me.Controls.Add(Me.Button16)
Me.Controls.Add(Me.Button15)
Me.Controls.Add(Me.Button14)
Me.Controls.Add(Me.Button13)
Me.Controls.Add(Me.Button12)
Me.Controls.Add(Me.Button11)
Me.Controls.Add(Me.Button10)
Me.Controls.Add(Me.Button9)
Me.Controls.Add(Me.Button8)
Me.Controls.Add(Me.Button7)
Me.Controls.Add(Me.Button6)
Me.Controls.Add(Me.Button5)
Me.Controls.Add(Me.Button4)
Me.Controls.Add(Me.Button3)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.TextBox1)
Me.Controls.Add(Me.Label1)
Me.Name = "SimpleCalculator"
Me.Text = "简单计算器"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
Private Sub TextBox1_TabStopChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim btn As Button '按钮类型的变量
btn = sender '把产生该事件的按钮对象赋值给btn
TextBox1.Text = TextBox1.Text + btn.Text '把该按钮的Text属性值连接到TextBox1中
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim btn As Button
btn = sender
TextBox1.Text += btn.Text
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim btn As Button
btn = sender
TextBox1.Text += btn.Text
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Dim btn As Button
btn = sender
TextBox1.Text += btn.Text
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
Dim btn As Button
btn = sender
TextBox1.Text += btn.Text
End Sub
Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
Dim btn As Button
btn = sender
TextBox1.Text += btn.Text
End Sub
Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
Dim btn As Button
btn = sender
TextBox1.Text += btn.Text
End Sub
Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
Dim btn As Button
btn = sender
TextBox1.Text += btn.Text
End Sub
Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
Dim btn As Button
btn = sender
TextBox1.Text += btn.Text
End Sub
Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
Dim btn As Button
btn = sender
TextBox1.Text += btn.Text
End Sub
Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click
Dim btn As Button
btn = sender
'在文本框的Text属性后连接一个空格、本按钮的Text属性值和一个空格
TextBox1.Text = TextBox1.Text + " " + btn.Text + " "
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox1.Text = "" '清空文本框
End Sub
Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button13.Click
Dim btn As Button
btn = sender
TextBox1.Text = TextBox1.Text + " " + btn.Text + " "
End Sub
Private Sub Button14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button14.Click
Dim btn As Button
btn = sender
TextBox1.Text = TextBox1.Text + " " + btn.Text + " "
End Sub
Private Sub Button15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button15.Click
Dim btn As Button
btn = sender
TextBox1.Text = TextBox1.Text + " " + btn.Text + " "
End Sub
Private Sub Button16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button16.Click
'发生错误转移到标号“ErrorPro”指定的行去执行错误处理程序
On Error GoTo ErrorPro
Dim r As Decimal '保存计算结果的变量
Dim t As String = TextBox1.Text '用于保存文本框中的算术表达式
Dim space As Integer = t.IndexOf(" ") '搜索空格位置,如果没有空格,返回值为0
'字符串的取子符方法,第二个参数小于0,则将产生错误号为5的异常,即过程参数不正确
Dim s1 As String = t.Substring(0, space) '通过取子串方法获得第一个运算数
Dim op As String = t.Substring(space + 1, 1) '通过取子串方法获得运算符
Dim s2 As String = t.Substring(space + 3) '通过取子串方法获得第二个运算数
Dim arg1, arg2 As Integer
arg1 = Val(s1) : arg2 = Val(s2)
Select Case op
Case "+" : r = arg1 + arg2
Case "-" : r = arg1 - arg2
Case "*" : r = arg1 * arg2
Case "/" : r = arg1 / arg2
Case Else
MsgBox("输入的运算符有误!")
Exit Sub
End Select
TextBox1.Text = CStr(r) '显示结果
Exit Sub '退出过程
ErrorPro: '错误处理程序块
Select Case Err.Number
Case 6 '除数为零时,或运算溢出时的错误号
MsgBox("算术运算溢出!", , "溢出提示")
TextBox1.Focus()
Exit Sub
Case 5 ' Substring过程的参数不符合要求的错误号
MsgBox("必须输入运算符和第二个运算数!", , "运算数少")
Exit Sub
Case Else
'其它情况显示错误号和错误原因
MsgBox("错误号为" Err.Number Chr(10) Chr(13) "错误原因:" Err.Description)
Exit Sub
End Select
End Sub
End Class
本文名称:vb.net求补码的简单介绍
本文URL:http://scgulin.cn/article/hdcghp.html