Main Menu

ROL and ROR bit operations

Started by kevin, June 03, 2023, 11:15:29 PM

Previous topic - Next topic

kevin

 ROL and ROR bit operations

     Even though PlayBASIC includes built in ROR and ROL functions (ROL32 and ROR32)  here's some example functions for those that have ever wondered how such binary operations work..  


PlayBASIC Code: [Select]
      Value=$AABBCCDD
for Shift=0 to 31
TestROL(Value,Shift)
next
sync
waitkey

Function TestROL(Value,Shift)
r1= ROL(Value,Shift)
r2=ROL32(Value,Shift)
print bin$(r1) +" "+Bin$(r2) +" Match="+str$(R1=R2)
EndFunction


Function ROR(Value,ShiftAmount)
ShiftAmount =ShiftAmount and 31
Result = (Value>> ShiftAmount) or (Value << (32-ShiftAmount))
ENdFunction Result

Function ROL(Value,ShiftAmount)
ShiftAmount =ShiftAmount and 31
Result = (Value<< ShiftAmount) or (Value >> (32-ShiftAmount))
ENdFunction Result