Page 1 of 1

ROL and ROR

Posted: Sun Jul 02, 2006 9:45 am
by blueznl
we have >> and <<but>>1 & $7B) | x<<7
[/code]

for a ror (roll over right) of a byte

may i suggest to add ROL and ROR to our ever increasing collection of great purebasic features? :-)

Posted: Sun Jul 02, 2006 1:35 pm
by SunSatION
Don't know if i can post here, but you can use ROR and ROL

Code: Select all

x = 10
ROL x,1
Debug x

Posted: Sun Jul 02, 2006 2:48 pm
by Dare
Second the request for native PureBasic commands/functions that support logical (versus Arithmetic) Shifts and Rotations.

It would make a nice addition to the language (along with unsigned integers :D)

Posted: Sun Jul 02, 2006 5:38 pm
by blueznl
i mean, look at the following, it's HORRIBLE

Code: Select all

Procedure.l x_rol(x.l)                                               ; rol a long
  ;
  ; *** shift all bits one left, move msb to bit 0
  ;
  ProcedureReturn (x<<1 | (x>>31 & $01))
EndProcedure

Procedure.l x_ror(x.l)                                               ; ror a long
  ;
  ; *** shift all bits one right, move lsb to bit 31
  ;
  ProcedureReturn ((x>>1 & $7FFFFFFF) | x<<31)
EndProcedure

Procedure.b x_rolb(x.b)                                              ; rol a byte
  ;
  ; *** shift all bits one left, move msb to bit 0
  ;
  ; need to either use an asm enhanced version of this routine, or pester fred into
  ; making it a primitive :-)
  ;
  ProcedureReturn (x<<1 | (x>>7 & $01))
EndProcedure

Procedure.b x_rorb(x.b)                                              ; ror a byte
  ;
  ; *** shift all bits one right, move lsb to bit 7
  ;
  ; need to either use an asm enhanced version of this routine, or pester fred into
  ; making it a primitive :-)
  ;
  ProcedureReturn ((x>>1 & $7F) | x<<7)
EndProcedure
horrible but the best i could come up with, and i do know nothing about asm so no luck there either... better solutions / work arounds are obviously welcome :-)

Posted: Sun Jul 02, 2006 9:14 pm
by SunSatION
So, check if I get it right, you want these operation on a 32-bit number?

Posted: Sun Jul 02, 2006 11:27 pm
by dioxin
Blueznl,
I don't get it.
What's wrong with SunSatION's suggestion? It should work on integers of any size, 8, 16 or 32 bits, it's neat and it's quick.

The fact it happens to be an ASM instruction is not a reason to avoid it. After all, if the compiler is written well then it's exactly what the compiler will convert your code into anyway.


Paul.

Posted: Sat Jul 15, 2006 2:50 pm
by Trond
dioxin wrote:The fact it happens to be an ASM instruction is not a reason to avoid it.
Now what happens on PowerPC with that x86 instruction? Also, it can't be used in an expression and you need to turn on inline asm, which has many annoyances.

Posted: Sat Jul 15, 2006 2:57 pm
by blueznl
dioxin, yes, you are right, but the whole idea of purebasic is that it 1. is a basic, and 2. is platform independent

we should not have to use asm or winapi to do basic functions, so yes it's no issue but i want to do it in native pb :-)

that's why it's a feature request :-)