正文

用VB进行移位操作2006-05-05 22:28:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/iamben250/13615.html

分享到:

用VB进行移位操作 VB没有提供移位操作的指令和函数,只提供and(与)、or(或)、xor(异或)、eqv(同或)、not(非)等几个运算符,而编程时有时需要对一个字节进行移位操作(如进行加密),怎么办?其实只用and、or二个运算符即可搞掂。例如要将变量byte1的第八位置1(假设byte1的二进制值为01001101),则只需byte1 or &h80 (即01001101 or 10000000),如要将第八位置0,则只需byte1 and &h7f。请看下面程序段是如何实现循环左移的:Public Function byteleft(byte1 As Byte, n As Integer) As Byte '将byte1左移n位Dim intem As Byte '临时变量Dim intem1 As Byte '临时变量Dim x, y As Integerintem1 = byte1For x = 1 To n '移多少位就循环多少次For y = 8 To 1 Step -1 '从第八位(左边第一位)开始循环左移Select Case yCase 8If (intem1 And &H80) = &H80 Then '如果临时变量intem1的第八位是1,intem = &H1 '则将临时变量intem置1,Elseintem = &H0 '反之置0End IfCase 7If (intem1 And &H40) = &H40 Then '如果临时变量intem1的第七位是1,intem1 = intem1 Or &H80 '则将其第八位置1(其它位不变),Elseintem1 = intem1 And &H7F '反之将第八位置0(其它位不变)End IfCase 6If (intem1 And &H20) = &H20 Then '操作与上面相同intem1 = intem1 Or &H40Elseintem1 = intem1 And &HBFEnd IfCase 5If (intem1 And &H10) = &H10 Thenintem1 = intem1 Or &H20Elseintem1 = intem1 And &HDFEnd IfCase 4If (intem1 And &H8) = &H8 Thenintem1 = intem1 Or &H10Elseintem1 = intem1 And &HEFEnd IfCase 3If (intem1 And &H4) = &H4 Thenintem1 = intem1 Or &H8Elseintem1 = intem1 And &HF7End IfCase 2If (intem1 And &H2) = &H2 Thenintem1 = intem1 Or &H4Elseintem1 = intem1 And &HFBEnd IfCase 1If (intem1 And &H1) = &H1 Thenintem1 = intem1 Or &H2Elseintem1 = intem1 And &HFDEnd IfIf intem = &H1 Then '移完第一位后,如果intem是1(即第八位是1)intem1 = intem1 Or &H1 '则将intem1的第一位置1Elseintem1 = intem1 And &HFE '反之置0End IfEnd SelectNext yNext xbyteleft = intem1 '将intem1的值返回给函数名End Function参照此程序段,不难实现循环右移。(此程序段在VB5上调试通过。)

阅读(3659) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册