SHL/SHR/ROL/ROR

BlitzMax Forums/BlitzMax Programming/SHL/SHR/ROL/ROR

GfK(Posted 2008) [#1]
If A = %00000001, and I perform an SHR on it, I expected (and needed) the result to be %10000000, but it isn't - the bits that drop off the end don't carry over to the left and the result is %00000000.

We don't seem to have ROL or ROR functions but I'm sure I've done this before. Anybody know how? :/


Azathoth(Posted 2008) [#2]
Write the function in assembly and import it into your blitzmax source?


TomToad(Posted 2008) [#3]
A = A Shr 1 + %10000000 * (A & 1)


Otus(Posted 2008) [#4]
Extern "C"
	
	Function Rol%(n% , b%) = "_rotl"
	Function Ror%(n% , b%) = "_rotr"
	
End Extern


They are for Ints, though.


PGF(Posted 2008) [#5]
Use something like this:

Local A:Int = $01
Local A_ROR1:Int = (A Shr 1) | ((A & $01) Shl 15)

Although Int is 32 bits, this is for 16 bit unsigned ROR. Adjust the Shl argument for some other number of bits but look out for sign and overflow behaviour if you are doing it for the full 32 bits.

You can also do more than 1 bit ROR / ROL in a single go like this:

Local A:Int = $F001
Local A_ROL3:Int = ((A & $1FFF) Shl 3) | ((A & $E000) Shr 13)

Again for 16 bit unsigned operation.

Put them in functions of course.


ImaginaryHuman(Posted 2008) [#6]
Function ROR:Int(Value:Int=0,Bits:Int=0)
      Return (Value Shr Bits) | ((Value Shl (32-Bits)) 'Rotate integer to the right
End Function

Function ROL:Int(Value:Int=0,Bits:Int=0)
      Return (Value Shl Bits) | ((Value Shr (32-Bits)) 'Rotate integer to the left
End Function



_33(Posted 2008) [#7]
Thanks ImaginaryHuman :)