Shop OBEX P1 Docs P2 Docs Learn Events
Bitwise Rotate Right -> — Parallax Forums

Bitwise Rotate Right ->

CassLanCassLan Posts: 586
edited 2009-06-05 08:57 in Propeller 1
Hi,

I have read the manual, but I can't seem to get rotate right to work:

CON
  _clkmode = xtal1 + pll16x
  _xinfreq = 5_000_000
 
VAR
  byte loopcounter
  word testpattern, testdone
 
OBJ
  text   : "tv_text"
 
PUB Main
  text.start(12)
  text.out(00)
  testpattern := %0111111111111111 
  repeat loopcounter from 0 to 9
      testdone := testpattern -> loopcounter
      text.bin(testdone,16)
      text.out(13)  


Produces:
0111111111111111
0011111111111111
0001111111111111
0000111111111111
0000011111111111
0000001111111111


That would make sense to me for a shift-right...but not rotate right.

Am I doing something wrong?

Thanks,

Rick

Comments

  • kuronekokuroneko Posts: 3,623
    edited 2009-06-04 13:22
    My guess would be that -> only works on longs not words. If you rotate a bit more, e.g. 31, you'll see the 1's coming in [noparse]:)[/noparse]
  • CassLanCassLan Posts: 586
    edited 2009-06-04 14:16
    I see what you mean, all of the examples in the manual are also using longs.

    Thanks kuroneko!


    Rick
  • Beau SchwabeBeau Schwabe Posts: 6,568
    edited 2009-06-04 14:45
    CassLan,

    I believe this will do what you want...

    CON
      _clkmode = xtal1 + pll16x
      _xinfreq = 5_000_000
     
    VAR
      byte loopcounter
      word testpattern
     
    OBJ
      text   : "tv_text"
     
    PUB Main
      text.start(12)
      text.out(00)
      testpattern := %0111111111111111
      repeat loopcounter from 0 to 9
          testdone := testpattern -> loopcounter
          testdone_low |= testdone_high
          testdone &= $FFFF
          text.bin(testdone,16)
          text.out(13)
    
    DAT
    
    testdone      long
    testdone_low  word      0
    testdone_high word      0
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.

    Post Edited (Beau Schwabe (Parallax)) : 6/4/2009 2:50:29 PM GMT
  • MagIO2MagIO2 Posts: 2,243
    edited 2009-06-04 20:10
    Hi Beau... ehem ... I disagree.

    I'd suggest:

    testdone_high := testpattern
    testdone_low:=testpattern
    testdone := testdone -> loopcounter & $ffff
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2009-06-04 20:41
    This will also work:

    testdone := testpattern >> loopcounter | testpattern << (16 - loopcounter)
    
    
    


    -Phil
  • Beau SchwabeBeau Schwabe Posts: 6,568
    edited 2009-06-04 21:06
    There is nothing really to disagree here, there are several methods that will work... take your pick

    Method 1
          testdone := testpattern -> loopcounter      ''     rotate 'testpattern' into 'testdone' by 'loopcounter' number of bits
                                                      ''testdone x000000000000000_0pxpxpxpxpxpxpxp
          
          testdone_low |= testdone_high               ''     OR highbyte of testdone into lowbyte of testdone
                                                      ''testdone x000000000000000_xpxpxpxpxpxpxpxp
                                                            
                                                      ''     Strip off upper 16 bits of testdone by ANDing with FFFF
           testdone &= $FFFF                          ''testdone 0000000000000000_xpxpxpxpxpxpxpxp
    
    
    



    Method 2
          testdone_high := testpattern                '' Move testpattern into highbyte of testdone
                                                      ''testdone pxpxpxpxpxpxpxpx_0000000000000000
    
          testdone_low  := testpattern                '' Move testpattern into lowbyte of testdone 
                                                      ''testdone pxpxpxpxpxpxpxpx_pxpxpxpxpxpxpxpx
                                                      
          testdone := testdone -> loopcounter & $ffff ''rotate 'testdone' by 'loopcounter' number of bits
                                                      ''testdone xpxpxpxpxpxpxpxp_xpxpxpxpxpxpxpxp
    
                                                      ''    Strip off upper 16 bits of testdone by ANDing with FFFF
                                                      ''testdone 0000000000000000_xpxpxpxpxpxpxpxp
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
  • MagIO2MagIO2 Posts: 2,243
    edited 2009-06-04 21:27
    Thank you Beau! You're right and I simply did not get it with a little extra explanation.

    One question ... if the rotate operation extends the operand to 32 bit, does it extend it sign correct? In this case method 1 would not work for 16 bit values starting with a MSB set to 1.

    Post Edited (MagIO2) : 6/4/2009 9:32:56 PM GMT
  • Beau SchwabeBeau Schwabe Posts: 6,568
    edited 2009-06-04 22:18
    MagIO2,

    "if the rotate operation extends the operand to 32 bit, does it extend it sign correct?" ... It's not extending it into 32 bit. The rotate is performed on a WORD, but only to read the WORD value. ALL of the bit manipulation is done within a 32-bit LONG internal register before it is written back to a variable. In this case the variable written back to was a LONG so all 32 bits were preserved. If it had been a WORD, the upper 16 bits would then be lost and the function would "act" like you were simply shifting the data and it would fall into nowhere, unless you shifted more than 16 bits. At that point you would start seeing the data once again if you were only writing the data back to a WORD variable.


    Example
    CON
      _clkmode = xtal1 + pll16x
      _xinfreq = 5_000_000
     
    VAR
      word testpattern,testdone
     
    OBJ
      text   : "tv_text"
     
    PUB Main
      text.start(12)
      text.out(00)
      testpattern  := %10000000_00000000
      testdone     := testpattern <- 17     'If this number is 1 to 16 the output is all Zeros.
      text.bin(testdone,16)
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.

    Post Edited (Beau Schwabe (Parallax)) : 6/4/2009 10:25:22 PM GMT
  • CassLanCassLan Posts: 586
    edited 2009-06-05 08:57
    Thanks all for the multiple solutions!

    Rick
Sign In or Register to comment.