查看完整版本: VB程序中实现字节移位操作

Nothing 2010-1-14 11:40

VB程序中实现字节移位操作

在利用VB系统开发数据采集或工业控制软件,或对文件进行低级操作时,常需要对字节进行移位操作,但VB系统中没有提供字节移位操作的指令和函数,只提供了And(与)、Or(或)、Xor(异或)、Equ(同或)、Not(非)等几个逻辑运算指令。笔者在用VB系统开发工业控制软件的过程中,就遇到了这个问题,于是利用VB中已有的逻辑运算指令,模拟汇编语言的字节移位指令,编制了七个字节移位函数:逻辑左移、逻辑右移、算术右移、循环左移、循环右移、进位循环左移和进位循环右移。

在汇编语言指令中,逻辑左移的功能相当于乘2, 逻辑右移的功能相当于除2,利用这个特性,在VB程序中用乘2和除2方法实现位的左移和右移,然后再用And(与)和Or(或)逻辑运算指令,判断移位过程中是否有进位发生,将进行标志置位。

程序清单
CF是进位标志,采用Boolean类型逻辑变量,如果CF为True表示有进位,为False则表示无进位。

[color=#000000][color=#0000ff]Public[/color] CF As Boolean [color=#0000cc]'[/color]进位标志

[color=#0000cc]'[/color]1[color=#0000cc].[/color]逻辑左移

[color=#0000ff]Public[/color] [color=#0000ff]Function[/color] SHL[color=#0000cc]([/color]OPR As Byte[color=#0000cc],[/color] n As Integer[color=#0000cc])[/color] As Byte
[color=#0000ff]Dim[/color] BD As Byte
[color=#0000ff]Dim[/color] I As Integer
BD [color=#0000cc]=[/color] OPR
[color=#0000ff]For[/color] i [color=#0000cc]=[/color] 1 [color=#0000ff]To[/color] n [color=#0000cc]-[/color] 1
BD [color=#0000cc]=[/color] [color=#0000cc]([/color]BD [color=#0000ff]And[/color] [color=#0000cc]&[/color]H7F[color=#0000cc])[/color] [color=#0000cc]*[/color] 2 [color=#0000cc]'[/color]将D7位屏蔽左移,防止字节溢出
[color=#0000ff]Next[/color] i
CF [color=#0000cc]=[/color] BD [color=#0000ff]And[/color] [color=#0000cc]&[/color]H80 [color=#0000cc]'[/color]判断D7位是否进位
SHL [color=#0000cc]=[/color] [color=#0000cc]([/color]BD [color=#0000ff]And[/color] [color=#0000cc]&[/color]H7F[color=#0000cc])[/color] [color=#0000cc]*[/color] 2
[color=#0000ff]End[/color] [color=#0000ff]Function[/color]


[color=#0000cc]'[/color]2[color=#0000cc].[/color]逻辑右移

[color=#0000ff]Public[/color] [color=#0000ff]Function[/color] SHR[color=#0000cc]([/color]OPR As Byte[color=#0000cc],[/color] n As Integer[color=#0000cc])[/color] As Byte
[color=#0000ff]Dim[/color] BD As Byte
[color=#0000ff]Dim[/color] I As Integer
BD [color=#0000cc]=[/color] OPR
[color=#0000ff]For[/color] i [color=#0000cc]=[/color] 1 [color=#0000ff]To[/color] n [color=#0000cc]-[/color] 1
BD [color=#0000cc]=[/color] BD [color=#0000cc]\[/color] 2 [color=#0000cc]'[/color]右移
[color=#0000ff]Next[/color] i
CF [color=#0000cc]=[/color] BD [color=#0000ff]And[/color] 1 [color=#0000cc]'[/color]判断D0位是否进位
SHR [color=#0000cc]=[/color] BD [color=#0000cc]\[/color] 2
[color=#0000ff]End[/color] [color=#0000ff]Function[/color]


[color=#0000cc]'[/color]3[color=#0000cc].[/color]算术右移

[color=#0000ff]Public[/color] [color=#0000ff]Function[/color] SAR[color=#0000cc]([/color]OPR As Byte[color=#0000cc],[/color] n As Integer[color=#0000cc])[/color] As Byte
[color=#0000ff]Dim[/color] BD As Byte
[color=#0000ff]Dim[/color] I As Integer
[color=#0000ff]Dim[/color] Fg1 As Byte
BD [color=#0000cc]=[/color] OPR
Fg1 [color=#0000cc]=[/color] BD [color=#0000ff]And[/color] [color=#0000cc]&[/color]H80
[color=#0000ff]For[/color] i [color=#0000cc]=[/color] 1 [color=#0000ff]To[/color] n [color=#0000cc]-[/color] 1
BD [color=#0000cc]=[/color] BD [color=#0000cc]\[/color] 2 [color=#0000cc]'[/color]右移
[color=#0000ff]Next[/color] i
CF [color=#0000cc]=[/color] BD [color=#0000ff]And[/color] 1 [color=#0000cc]'[/color]判断D0位是否进位
BD [color=#0000cc]=[/color] BD [color=#0000cc]\[/color] 2 [color=#0000cc]'[/color]右移
SAR [color=#0000cc]=[/color] BD [color=#0000ff]Or[/color] Fg1
[color=#0000ff]End[/color] [color=#0000ff]Function[/color]


[color=#0000cc]'[/color]4[color=#0000cc].[/color]循环左移

[color=#0000ff]Public[/color] [color=#0000ff]Function[/color] ROL[color=#0000cc]([/color]OPR As Byte[color=#0000cc],[/color] n As Integer[color=#0000cc])[/color] As Byte
[color=#0000ff]Dim[/color] BD As Byte
[color=#0000ff]Dim[/color] I As Integer
[color=#0000ff]Dim[/color] Fg1 As Byte
BD [color=#0000cc]=[/color] OPR
[color=#0000ff]For[/color] i [color=#0000cc]=[/color] 1 [color=#0000ff]To[/color] n
Fg1 [color=#0000cc]=[/color] [color=#0000cc]([/color]BD [color=#0000ff]And[/color] [color=#0000cc]&[/color]H80[color=#0000cc])[/color] [color=#0000cc]\[/color] 128 [color=#0000cc]'[/color]判断D7位是否进位
BD [color=#0000cc]=[/color] [color=#0000cc]([/color][color=#0000cc]([/color]BD [color=#0000ff]And[/color] [color=#0000cc]&[/color]H7F[color=#0000cc])[/color] [color=#0000cc]*[/color] 2[color=#0000cc])[/color] [color=#0000ff]Or[/color] Fg1 [color=#0000cc]'[/color]带进位左移
[color=#0000ff]Next[/color] i
CF [color=#0000cc]=[/color] Fg1
ROL [color=#0000cc]=[/color] BD
[color=#0000ff]End[/color] [color=#0000ff]Function[/color]


[color=#0000cc]'[/color]5[color=#0000cc].[/color]循环右移

[color=#0000ff]Public[/color] [color=#0000ff]Function[/color] ROR[color=#0000cc]([/color]OPR As Byte[color=#0000cc],[/color] n As Integer[color=#0000cc])[/color] As Byte
[color=#0000ff]Dim[/color] BD As Byte
[color=#0000ff]Dim[/color] I As Integer
[color=#0000ff]Dim[/color] Fg1 As Byte
[color=#0000ff]Dim[/color] Fg2 As Byte
BD [color=#0000cc]=[/color] OPR
[color=#0000ff]For[/color] i [color=#0000cc]=[/color] 1 [color=#0000ff]To[/color] n
Fg1 [color=#0000cc]=[/color] [color=#0000cc]([/color]BD [color=#0000ff]And[/color] 1[color=#0000cc])[/color] [color=#0000cc]*[/color] 128 [color=#0000cc]'[/color]判断D0位是否进位
BD [color=#0000cc]=[/color] [color=#0000cc]([/color]BD [color=#0000cc]\[/color] 2[color=#0000cc])[/color] [color=#0000ff]Or[/color] Fg1 [color=#0000cc]'[/color]带进位右移
[color=#0000ff]Next[/color] i
CF [color=#0000cc]=[/color] Fg1
ROR [color=#0000cc]=[/color] BD
[color=#0000ff]End[/color] [color=#0000ff]Function[/color]


[color=#0000cc]'[/color]6[color=#0000cc].[/color]进位循环左移

[color=#0000ff]Public[/color] [color=#0000ff]Function[/color] RCL[color=#0000cc]([/color]OPR As Byte[color=#0000cc],[/color] n As Integer[color=#0000cc])[/color] As Byte
[color=#0000ff]Dim[/color] BD As Byte
[color=#0000ff]Dim[/color] I As Integer
[color=#0000ff]Dim[/color] Fg1 As Byte
[color=#0000ff]Dim[/color] Fg2 As Byte
BD [color=#0000cc]=[/color] OPR
Fg2 [color=#0000cc]=[/color] CF [color=#0000ff]And[/color] 1
[color=#0000ff]For[/color] i [color=#0000cc]=[/color] 1 [color=#0000ff]To[/color] n
Fg1 [color=#0000cc]=[/color] [color=#0000cc]([/color]BD [color=#0000ff]And[/color] [color=#0000cc]&[/color]H80[color=#0000cc])[/color] [color=#0000cc]\[/color] 128 [color=#0000cc]'[/color]判断D7位是否进位
BD [color=#0000cc]=[/color] [color=#0000cc]([/color][color=#0000cc]([/color]BD [color=#0000ff]And[/color] [color=#0000cc]&[/color]H7F[color=#0000cc])[/color] [color=#0000cc]*[/color] 2[color=#0000cc])[/color] [color=#0000ff]Or[/color] Fg2 [color=#0000cc]'[/color]带进位左移
Fg2 [color=#0000cc]=[/color] Fg1
[color=#0000ff]Next[/color] i
CF [color=#0000cc]=[/color] Fg1
RCL [color=#0000cc]=[/color] BD
[color=#0000ff]End[/color] [color=#0000ff]Function[/color]


[color=#0000cc]'[/color]7[color=#0000cc].[/color]进位循环右移

[color=#0000ff]Public[/color] [color=#0000ff]Function[/color] RCR[color=#0000cc]([/color]OPR As Byte[color=#0000cc],[/color] n As Integer[color=#0000cc])[/color] As Byte
[color=#0000ff]Dim[/color] BD As Byte
[color=#0000ff]Dim[/color] I As Integer
[color=#0000ff]Dim[/color] Fg1 As Byte
[color=#0000ff]Dim[/color] Fg2 As Byte
BD [color=#0000cc]=[/color] OPR
Fg2 [color=#0000cc]=[/color] CF [color=#0000ff]And[/color] 128
[color=#0000ff]For[/color] i [color=#0000cc]=[/color] 1 [color=#0000ff]To[/color] n
Fg1 [color=#0000cc]=[/color] [color=#0000cc]([/color]BD [color=#0000ff]And[/color] 1[color=#0000cc])[/color] [color=#0000cc]*[/color] 128 [color=#0000cc]'[/color]判断D0位是否进位
BD [color=#0000cc]=[/color] [color=#0000cc]([/color]BD [color=#0000cc]\[/color] 2[color=#0000cc])[/color] [color=#0000ff]Or[/color] Fg2 [color=#0000cc]'[/color]带进位右移
Fg2 [color=#0000cc]=[/color] Fg1
[color=#0000ff]Next[/color] i
CF [color=#0000cc]=[/color] Fg1
RCR [color=#0000cc]=[/color] BD
[color=#0000ff]End[/color] [color=#0000ff]Function[/color]
[/color]


结束语
以上七个字节移位操作函数的功能和用法同宏汇编语言的移位操作指令基本相同,不过只对单字节进行操作,但上述程序稍加改动后,就可对双字节Integer类型和四字节Long类型进行移位操作。
页: [1]
查看完整版本: VB程序中实现字节移位操作