### Author Topic: Bit Shifting  (Read 5069 times)

#### John

• Forum Support / SB Dev
• Posts: 2983
##### 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"' 2PRINT rotates(4,32),"\n"' 4PRINT rotates(8,-32),"\n"' 8`
jrs@laptop:~/sb/sb22/sdl\$ scriba rotates.sb
2
0
0
jrs@laptop:~/sb/sb22/sdl\$

#### 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 »

#### John

• Forum Support / SB Dev
• Posts: 2983
##### 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"' 2PRINT rotates(4,32),"\n"' 4PRINT 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 »

#### John

• Forum Support / SB Dev
• Posts: 2983
##### 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.

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 »

#### John

• Forum Support / SB Dev
• Posts: 2983
##### 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"NEXTjrs@laptop:~/sb/sb22/sdl\$ time scriba testrot.sb326412825651210242048409681921638432768655361310722621445242881048576209715241943048388608167772163355443267108864134217728268435456536870912107374182421474836481248163264128256512102420484096819216384327686553613107226214452428810485762097152419430483886081677721633554432671088641342177282684354565368709121073741824214748364812481632real 0m0.010suser 0m0.000ssys 0m0.008sjrs@laptop:~/sb/sb22/sdl\$ `
« Last Edit: February 01, 2014, 03:01:38 PM by John »