VB logical shift

Source: Internet
Author: User
When using the VB system to develop data collection or industrial control software, or perform low-level operations on files, it is often necessary to shift the bytes, however, the VB system does not provide instructions and functions for the byte shift operation, and only provides and (and), or (OR), XOR (exclusive or), equ (same or) and not (not. The author encountered this problem when developing industrial control software using the VB system, so he used the existing logic operation commands in VB to simulate the byte shift commands of the assembly language, the seven-byte Shift functions are compiled: Logical left shift, logical right shift, arithmetic right shift, cyclic left shift, cyclic right shift, carry loop left shift, and inbound loop right shift.



In assembly language commands, the left-shift logic function is equivalent to multiplication 2, and the right-shift logic function is equivalent to Division 2. With this feature, in the vbprogram, we use the multiplication 2 and Division 2 methods to shift the Left and Right of BITs, and then use the and (and) and or (OR) logic operation commands to determine whether there is an increment in the shift process, the flag is set.



Program list

Cf indicates the carry flag. logical variables of the boolean type are used. If CF is true, it indicates that there is a carry-in. If it is false, it indicates that there is no carry-in.



Public cf as Boolean 'carry flag



'1. Logical left shift



Public Function SHL (OPR as byte, N as integer) as byte

Dim BD as byte

Dim I as integer

BD = OPR

For I = 1 to n-1

BD = (BD and & 0000f) * 2' blocks the D7 bit from the Left to prevent byte overflow

Next I

Cf = BD and & h80' determine whether the D7 bit is carried

SHL = (BD and & h7f) * 2

End Function





'2. Right Shift of Logic



Public Function SHR (OPR as byte, N as integer) as byte

Dim BD as byte

Dim I as integer

BD = OPR

For I = 1 to n-1

BD = BD/2' shift right

Next I

Cf = BD and 1' determine whether the D0 bits are carried

SHR = BD/2

End Function





'3. Right Shift of arithmetic



Public Function SAR (OPR as byte, N as integer) as byte

Dim BD as byte

Dim I as integer

Dim FG1 as byte

BD = OPR

FG1 = BD and & h80

For I = 1 to n-1

BD = BD/2' shift right

Next I

Cf = BD and 1' determine whether the D0 bits are carried

BD = BD/2' shift right

SAR = BD or FG1

End Function





'4. Move the loop left



Public Function ROL (OPR as byte, N as integer) as byte

Dim BD as byte

Dim I as integer

Dim FG1 as byte

BD = OPR

For I = 1 to n

FG1 = (BD and & h80)/100' to determine whether the D7 bit is carried

BD = (BD and & quot; f) * 2) or FG1 'bring into the left shift

Next I

Cf = FG1

Rol = BD

End Function





'5. Move the loop right



Public Function ror (OPR as byte, N as integer) as byte

Dim BD as byte

Dim I as integer

Dim FG1 as byte

Dim FG2 as byte

BD = OPR

For I = 1 to n

FG1 = (BD and 1) * 100' determine whether the D0 bit is carried

BD = (BD/2) or FG1 'bring in right shift

Next I

Cf = FG1

Ror = BD

End Function





'6. Carry loop shifts left



Public Function RCL (OPR as byte, N as integer) as byte

Dim BD as byte

Dim I as integer

Dim FG1 as byte

Dim FG2 as byte

BD = OPR

FG2 = CF and 1

For I = 1 to n

FG1 = (BD and & h80)/100' to determine whether the D7 bit is carried

BD = (BD and & quot; f) * 2) or FG2 'bring to left shift

FG2 = FG1

Next I

Cf = FG1

RCL = BD

End Function





'7. Right Shift of carry Loop



Public Function RCR (OPR as byte, N as integer) as byte

Dim BD as byte

Dim I as integer

Dim FG1 as byte

Dim FG2 as byte

BD = OPR

FG2 = CF and 128

For I = 1 to n

FG1 = (BD and 1) * 100' determine whether the D0 bit is carried

BD = (BD/2) or FG2 'bring in right shift

FG2 = FG1

Next I

Cf = FG1

RCR = BD

End Function





Conclusion

The functions and usage of the above seven-byte shift operation functions are basically the same as that of the macro assembly language shift operation commands, but only the single-byte operation, but after a slight change to the above program, you can shift the two-byte Integer type and four-byte long type.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.