在利用VB系统开发数据采集或工业控制软件,或对文件进行低级 *** 作时,常需要对字节进行移位 *** 作,但VB系统中没有提供字节移位 *** 作的指令和函数,只提供了And(与)、Or(或)、Xor(异或)、Equ(同或)、Not(非)等几个逻辑运算指令。笔者在用VB系统开发工业控制软件的过程中,就遇到了这个问题,于是利用VB中已有的逻辑运算指令,模拟汇编语言的字节移位指令,编制了七个字节移位函数:逻辑左移、逻辑右移、算术右移、循环左移、循环右移、进位循环左移和进位循环右移。 在汇编语言指令中,逻辑左移的功能相当于乘2,逻辑右移的功能相当于除2,利用这个特性,在VB程序中用乘2和除2方法实现位的左移和右移,然后再用And(与)和Or(或)逻辑运算指令,判断移位过程中是否有进位发生,将进行标志置位。 程序清单 CF是进位标志,采用Boolean类型逻辑变量,如果CF为True表示有进位,为False则表示无进位。 Public CF As Boolean '进位标志 '1.逻辑左移 Public Function SHL(OPR As Byte,n As Integer) As ByteDim BD As ByteDim I As IntegerBD = OPRFor i = 1 To n - 1BD = (BD And &H7F) * 2 '将D7位屏蔽左移,防止字节溢出Next iCF = BD And &H80 '判断D7位是否进位SHL = (BD And &H7F) * 2End Function '2.逻辑右移 Public Function SHR(OPR As Byte,n As Integer) As ByteDim BD As ByteDim I As IntegerBD = OPRFor i = 1 To n - 1BD = BD / 2 '右移Next iCF = BD And 1 '判断D0位是否进位SHR = BD / 2End Function '3.算术右移 Public Function SAR(OPR As Byte,n As Integer) As ByteDim BD As ByteDim I As IntegerDim Fg1 As ByteBD = OPRFg1 = BD And &H80For i = 1 To n - 1BD = BD / 2 '右移Next iCF = BD And 1 '判断D0位是否进位BD = BD / 2 '右移SAR = BD Or Fg1End Function '4.循环左移 Public Function Rol(OPR As Byte,n As Integer) As ByteDim BD As ByteDim I As IntegerDim Fg1 As ByteBD = OPRFor i = 1 To nFg1 = (BD And &H80) / 128 '判断D7位是否进位BD = ((BD And &H7F) * 2) Or Fg1 '带进位左移Next iCF = Fg1Rol = BDEnd Function '5.循环右移 Public Function ROR(OPR As Byte,n As Integer) As ByteDim BD As ByteDim I As IntegerDim Fg1 As ByteDim Fg2 As ByteBD = OPRFor i = 1 To nFg1 = (BD And 1) * 128 '判断D0位是否进位BD = (BD / 2) Or Fg1 '带进位右移Next iCF = Fg1ROR = BDEnd Function '6.进位循环左移 Public Function RCL(OPR As Byte,n As Integer) As ByteDim BD As ByteDim I As IntegerDim Fg1 As ByteDim Fg2 As ByteBD = OPRFg2 = CF And 1For i = 1 To nFg1 = (BD And &H80) / 128 '判断D7位是否进位BD = ((BD And &H7F) * 2) Or Fg2 '带进位左移Fg2 = Fg1Next iCF = Fg1RCL = BDEnd Function '7.进位循环右移 Public Function RCR(OPR As Byte,n As Integer) As ByteDim BD As ByteDim I As IntegerDim Fg1 As ByteDim Fg2 As ByteBD = OPRFg2 = CF And 128For i = 1 To nFg1 = (BD And 1) * 128 '判断D0位是否进位BD = (BD / 2) Or Fg2 '带进位右移Fg2 = Fg1Next iCF = Fg1RCR = BDEnd Function 结束语 以上七个字节移位 *** 作函数的功能和用法同宏汇编语言的移位 *** 作指令基本相同,不过只对单字节进行 *** 作,但上述程序稍加改动后,就可对双字节Integer类型和四字节Long类型进行移位 *** 作。总结
以上是内存溢出为你收集整理的VB 逻辑移位全部内容,希望文章能够帮你解决VB 逻辑移位所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)