1、vb程序中实现字节移位操作(Byte shift operation in VB program)Author: Lonely Nine SwordEmail: Date: 2001-7-5 19:37:15Byte shift operation in VB program(Li Xiangjiang, 2001, 04, 29, 11:34)In the use of VB data acquisition system development or industrial control software, or low-level operations on the file, ofte
2、n requires a byte shift operation, but the VB system does not provide byte shift operation instructions and functions, provides only And (and Or), and (or) Xor (XOR) and Equ (or the same), Not (non -) and several other logic operations. The author used VB in the development of industrial control sys
3、tem software in the process, encountered this problem, so the use of VB in the logical operation instruction, simulation assembly language byte shift instruction program seven byte shift function: logic shift left, right, right, circular logic arithmetic left shift, rotate right, carry cycle left sh
4、ift and rotate right.In assembler language instruction function, logic shift left equivalent by 2, logic shift function is equivalent to only 2, using this feature in the VB program by 2 and in addition to the 2 methods to achieve the left and right, and then And (and Or) and (or) logic operations,
5、judgment if there is a shift in the process of carry, the flag is set.Program listCF is a carry flag, which uses Boolean type logical variables. If CF is True, there is a carry, and False means no carry.Public CF As Boolean carry flag1. logic moves leftPublic Function SHL (OPR As Byte, n As Integer)
6、 As ByteDim BD As ByteDim I As IntegerBD = OPRFor I = 1, To, N - 1BD = (BD And &H7F) * 2 moves the D7 bit to the left to prevent byte overflowNext ICF = BD, And, &H80 determines whether the D7 bit is carried or notSHL = (BD, And, &H7F) * 2End Function2. logical right shiftPublic Function SHR (OPR As
7、 Byte, n As Integer) As ByteDim BD As ByteDim I As IntegerBD = OPRFor I = 1, To, N - 1BD = BD 2 right shiftNext ICF = BD, And 1 determines whether the D0 bit is carriedSHR = BD 2End Function3. arithmetic right shiftPublic Function SAR (OPR As Byte, n As Integer) As ByteDim BD As ByteDim I As Integer
8、Dim Fg1 As ByteBD = OPRFg1 = BD, And, &H80For I = 1, To, N - 1BD = BD 2 right shiftNext ICF = BD And 1 determines whether the D0 bit is carried or notBD = BD 2 right shiftSAR = BD, Or, Fg1End Function4. loop left shiftPublic Function ROL (OPR As Byte, n As Integer) As ByteDim BD As ByteDim I As Inte
9、gerDim Fg1 As ByteBD = OPRFor I = 1, To, nFg1 = (BD And &H80) 128 determines whether D7 bit is carried or notBD = (BD And &H7F) * 2) Or Fg1 left with carryNext ICF = Fg1ROL = BDEnd Function5. loop right shiftPublic Function ROR (OPR As Byte, n As Integer) As ByteDim BD As ByteDim I As IntegerDim Fg1
10、 As ByteDim Fg2 As ByteBD = OPRFor I = 1, To, nFg1 = (BD And 1) * 128 determines whether D0 bit is carried or notBD = (BD 2) Or Fg1 move right with carryNext ICF = Fg1ROR = BDEnd Function6. carry loop left shiftPublic Function RCL (OPR As Byte, n As Integer) As ByteDim BD As ByteDim I As IntegerDim
11、Fg1 As ByteDim Fg2 As ByteBD = OPRFg2 = CF And 1For I = 1 To nFg1 = (BD And &H80) 128 determines whether D7 bit is carried or notBD = (BD And &H7F) * 2) Or Fg2 left with carryFg2 = Fg1Next ICF = Fg1RCL = BDEnd Function7. carry loop right shiftPublic Function RCR (OPR As Byte, n As Integer) As ByteDi
12、m BD As ByteDim I As IntegerDim Fg1 As ByteDim Fg2 As ByteBD = OPRFg2 = CF And 128For I = 1, To, nFg1 = (BD And 1) * 128 determines whether D0 bit is carried or notBD = (BD 2) Or Fg2 move right with carryFg2 = Fg1Next ICF = Fg1RCR = BDEnd FunctionConcluding remarksThe function and usage of the above seven byte shift operation function with macro assembler language shift instruction is basically the same, but only for single byte operation, but the program is modified, can be a double byte and four byte Integer type Long type shift operation.