Author Topic: Bit Shifting  (Read 4272 times)

Offline John

  • Forum Support / SB Dev
  • Posts: 2747
    • ScriptBasic Open Source Project
Re: Bit Shifting
« Reply #15 on: February 01, 2014, 08:53:26 AM »
That doesn't seem to work.

Code: [Select]
DECLARE SUB rotates ALIAS "SB_rotates" LIB "sdl"

PRINT rotates(0x80000000,2),"\n"
' 2
PRINT rotates(4,32),"\n"
' 4
PRINT rotates(8,-32),"\n"
' 8

jrs@laptop:~/sb/sb22/sdl$ scriba rotates.sb
2
0
0
jrs@laptop:~/sb/sb22/sdl$

Offline Charles Pegge

  • BASIC Developer
  • Posts: 69
Re: Bit Shifting
« Reply #16 on: February 01, 2014, 09:33:22 AM »
Ah, yes -32 and +32 are null shifts, then y does not get processed

Try replacing y with x here:
 
  besRETURN_LONG(x & 0xffffffff);

then try tests with +- 31 bits.
« Last Edit: February 01, 2014, 09:37:02 AM by Charles Pegge »

Offline John

  • Forum Support / SB Dev
  • Posts: 2747
    • ScriptBasic Open Source Project
Re: Bit Shifting
« Reply #17 on: February 01, 2014, 09:42:27 AM »
Success!

Code: [Select]
besFUNCTION(SB_rotates)
  DIM AS int co, v, x, y, d, p, ar;
  besARGUMENTS("ii[i]")
    AT v, AT p, AT ar
  besARGEND
  IF (ar EQ NULL) THEN_DO ar = 0;
  x = 0xffffffff & v;
  d = p & 0x1f;
  FOR (co = 1 TO co <= d STEP INCR co)
  BEGIN_FOR
    y = x << 1;
    IF (x & 0x80000000) THEN_DO y |= 1;
    x = y;
  NEXT
  besRETURN_LONG(x & 0xffffffff);
besEND

Code: [Select]
DECLARE SUB rotates ALIAS "SB_rotates" LIB "sdl"

PRINT rotates(0x80000000,2),"\n"
' 2
PRINT rotates(4,32),"\n"
' 4
PRINT rotates(8,-32),"\n"
' 8

jrs@laptop:~/sb/sb22/sdl$ scriba rotates.sb
2
4
8
jrs@laptop:~/sb/sb22/sdl$

Code: [Select]
DECLARE SUB rotates ALIAS "SB_rotates" LIB "sdl"

PRINT rotates(0x80000000,2),"\n"
PRINT rotates(4,31),"\n"
PRINT rotates(8,-31),"\n"

jrs@laptop:~/sb/sb22/sdl$ scriba rotates.sb
2
2
16
jrs@laptop:~/sb/sb22/sdl$
« Last Edit: February 01, 2014, 10:51:54 AM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 2747
    • ScriptBasic Open Source Project
Re: Bit Shifting
« Reply #18 on: February 01, 2014, 01:55:45 PM »
Charles,

I remove the third argument from the rotates() routine as it's not used in your code. Thanks again for all the help with this. I hope it got your C BASIC juices flowing again.  :-X

Code: [Select]
besFUNCTION(SB_rotates)
  DIM AS int co, v, x, y, d, p;
  besARGUMENTS("ii")
    AT v, AT p
  besARGEND
  x = 0xffffffff & v;
  d = p & 0x1f;
  FOR (co = 1 TO co <= d STEP INCR co)
  BEGIN_FOR
    y = x << 1;
    IF (x & 0x80000000) THEN_DO y |= 1;
    x = y;
  NEXT
  besRETURN_LONG(x & 0xffffffff);
besEND
« Last Edit: February 01, 2014, 02:23:36 PM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 2747
    • ScriptBasic Open Source Project
Re: Bit Shifting
« Reply #19 on: February 01, 2014, 02:57:36 PM »
A bit of rotate fun.

Code: [Select]
DECLARE SUB rotates ALIAS "SB_rotates" LIB "sdl"

FOR x = -32 to 32
  PRINT rotates(32,x),"\n"
NEXT

jrs@laptop:~/sb/sb22/sdl$ time scriba testrot.sb
32
64
128
256
512
1024
2048
4096
8192
16384
32768
65536
131072
262144
524288
1048576
2097152
4194304
8388608
16777216
33554432
67108864
134217728
268435456
536870912
1073741824
2147483648
1
2
4
8
16
32
64
128
256
512
1024
2048
4096
8192
16384
32768
65536
131072
262144
524288
1048576
2097152
4194304
8388608
16777216
33554432
67108864
134217728
268435456
536870912
1073741824
2147483648
1
2
4
8
16
32

real 0m0.010s
user 0m0.000s
sys 0m0.008s
jrs@laptop:~/sb/sb22/sdl$
« Last Edit: February 01, 2014, 03:01:38 PM by John »