In assembly language there are the rol and, ror statements to do that.
The rotate_procs.bi file contains the FB rotl() and, rotr() Functions for:
ULong or ULongInt data-types (overloaded and private).
This means:
- If only the one Function is used, the others are NOT included in the binary.
- The compiler picks the appropriate Function to use, depending on variable size.
Usage:
- rotl(v_in, rbit) or rotr(v_in, rbit)
Where:
v_in = value/variable to manipulate
rbit = number of bits to rotate (optional, default = 1)
rotate_procs.bi:
Code: Select all
' rotate_procs.bi -- (c) 2021-01-06, MrSwiss
#Ifndef __ROTATE_PROCS_BI__
#Define __ROTATE_PROCS_BI__
' the same as: rol (assembly statement) rotate bits to left
Declare Function rotl OverLoad(ByVal As ULong, ByVal As ULong=1) As ULong
Declare Function rotl OverLoad(ByVal As ULongInt, ByVal As ULong=1) As ULongInt
' the same as: ror (assembly statement) rotate bits to right
Declare Function rotr OverLoad(ByVal As ULong, ByVal As ULong=1) As ULong
Declare Function rotr OverLoad(ByVal As ULongInt, ByVal As ULong=1) As ULongInt
Private Function rotl OverLoad( _ ' rotate left
ByVal v_in As ULong, _ ' value to rotate (32 bits)
ByVal rbit As ULong = 1 _ ' _opt_ rotation in bits (default: 1)
) As ULong
If rbit < 1 OrElse rbit > 31 Then Return 0
Return (v_in Shl rbit) Or (v_in Shr (32 - rbit))
End Function
Private Function rotl OverLoad( _ ' as above
ByVal v_in As ULongInt, _ ' value to rotate (64 bits)
ByVal rbit As ULong = 1 _ ' as above
) As ULongInt
If rbit < 1 OrElse rbit > 63 Then Return 0
Return (v_in Shl rbit) Or (v_in Shr (64 - rbit))
End Function
Private Function rotr OverLoad( _ ' rotate right
ByVal v_in As ULong, _ ' value to rotate (32 bits)
ByVal rbit As ULong = 1 _ ' _opt_ rotation in bits (default: 1)
) As ULong
If rbit < 1 OrElse rbit > 31 Then Return 0
Return (v_in Shr rbit) Or (v_in Shl (32 - rbit))
End Function
Private Function rotr OverLoad( _ ' as above
ByVal v_in As ULongInt, _ ' value to rotate (64 bits)
ByVal rbit As ULong = 1 _ ' as above
) As ULongInt
If rbit < 1 OrElse rbit > 63 Then Return 0
Return (v_in Shr rbit) Or (v_in Shl (64 - rbit))
End Function
#EndIf ' __ROTATE_PROCS_BI__
' ----- EOF -----
Code: Select all
' rotate_procs.bas -- (c) 2021-01-06, MrSwiss
#Include "rotate_procs.bi"
' DEMO code (only the 32-bit variants are used here)
' it proves that: rotl() has invers proportional results _
' to rotr() and, vice versa (second part of demo)
Dim As Long test = &h8091A2B3
? "test: "; test, Bin(test, 32)
?
For i As UInteger = 1 To 31 ' 0 or 32 throw ERROR = 0
? "rotl("; test; ", "; i; "): ", Bin(rotl(test, i), 32)
Next
? : ? "press a key to continue ... "
Sleep : ? : ?
? "test: "; test, Bin(test, 32)
?
For i As UInteger = 1 To 31 ' 0 or 32 throw ERROR = 0
? "rotl("; test; ", "; i; "): ", Bin(rotl(test, i), 32)
Var inv = 32 - i
? "rotr("; test; ", "; inv; "): ", Bin(rotr(test, inv), 32)
?
Next
? : ? "... done ... ";
Sleep
' ----- EOF -----