Shr Function

Purpose

Shifts a bit pattern right. Shr can be used as a function, as an operator, and as an assignment operator.

Syntax

Shr(m, n)

Shr%(m, n)

Shr&(m, n)n

Shr|(m, n)

Shr8(m, n)

m Shr n

m Shr8 n

Shr v, n

m, n:integer expression
v:ivar

Description

Shr(m, n) and Shr% shifts the bit pattern of a 32-bit integer expressions m, n places right (Shr = SHift Right) and, optionally, stores the new value in a variable. Shr&(m, n) and Shr|(m, n) shift the bit pattern of a 16-bit or an 8-bit integer expression m respectively, n places right. Shr8 is used to shift a Large integer.

The operators Shr and Shr8 perform a right shift on an integer and Large, respectively.

Shr v, n assignment shifts the value in v by n and returns the value in v. The type of the operation is determined by the type of variable v.

Example

Debug.Show

Dim l|, l%

Trace Bin$(202, 16)             // Prints 0000000011001010

Trace Bin$(Shr(202, 4), 16)     // Prints 0000000000001100

l% = Shr(202, 4)

Trace Bin$(Shr%(202, 4), 16)    // Prints 0000000000001100

l% = Shr%(202, 4)

Trace Bin$(Shr|(202, 4), 8)     // Prints 00001100

l| = Shr(202, 4)

Trace l|                        // Prints 12

Remarks

m >> n is synonymous with Shr(m, n) and can be used instead. As long as the result of the shift does not exceed the given width, Shr(m, n) is equivalent to a division of m by 2^n.

x = 100 : 100 Shr 3 or Shr(100, 3)

100 in binary: 0000 0000 0000 0000 0000 0000 0110 0100
Shift: 0000 0000 0000 0000 0000 0000 0011 0010
Shift: 0000 0000 0000 0000 0000 0000 0001 1001
Shift: 0000 0000 0000 0000 0000 0000 0000 1100

Result is 12 = CInt(100 / 8) = CInt(100 / 2^3)

x = -8 : -8 Shr 4 or Shr(-8, 4)

-8 in binary: 1111 1111 1111 1111 1111 1111 1111 0111
Shift: 0111 1111 1111 1111 1111 1111 1111 1011
Shift: 0011 1111 1111 1111 1111 1111 1111 1101
Shift: 0001 1111 1111 1111 1111 1111 1111 1110
Shift: 0000 1111 1111 1111 1111 1111 1111 1111

Result is 258435455.

See Also

Shl, Rol, Ror, <<, >>

{Created by Sjouke Hamstra; Last updated: 23/10/2014 by James Gaite}