Shop OBEX P1 Docs P2 Docs Learn Events
What is the spin equivalent — Parallax Forums

What is the spin equivalent

In C you can do

var = ~var 

but it doesn't work in spin 2. I tried

var := !var 

and it makes 8 bits become 32 bits.

Comments

  • JonnyMacJonnyMac Posts: 8,927
    edited 2021-02-25 23:07

    ! is the correct operator. Spin does not have a way to recast variable size. I tested on a byte and then output 16 bits -- the upper eight bits were 0 (hence not affected).

  • here is the scenario:

    lets say val = %10000000

    in c val = ~val would yield %01111111

    in spin2 val = !val would yield %11111111_11111111_11111111_011111111

  • RaymanRayman Posts: 13,898

    Is var declared as a byte in a var section ?

  • No.

    in PUB i initialize it as byte[@val] := $00

  • JonnyMacJonnyMac Posts: 8,927
    edited 2021-02-25 23:40

    That will only happen, Greg, when your variable is a long. As I stated earlier, Spin cannot recast variables. If you declare your variable as a byte it will stay a byte -- and remember, locals are longs unless you explicitly define them as bytes or words.

    Here's a screen-shot of my test.

  • I changed it to byte on the pub line. That fixed part of the problem.

    val = %10000000

    val = !val
    now val = %1111111

    here is the code

    first debug in else prints out %10000000

    second debug in else prints out %1111111

    PUB setLed(addr, row, column, state) | byte val, offset
    
      val := $00
    
      if(addr<0 || addr>=maxDevices)
        return
    
      if(row<0 || row>7 || column<0 || column>7)
        return
    
      offset := addr * 8
      val := %10000000 >> column
    
      if(state)
        status[offset+row] := status[offset+row]|val
      else
        debug (ubin(val))
        val := !val
        debug ("fliped ",ubin(val))
        status[offset+row] := status[offset+row]&val
    
      spiTransfer(addr, row+1, status[offset+row])
    
  • RaymanRayman Posts: 13,898

    Looks like it flipped all the bits. Isn't that the right thing to do?

  • It's missing a 0 at the beginning of the second debug. the value after the !val should be %01111111

  • RaymanRayman Posts: 13,898

    Debug may not be showing you that leading 0

  • Greg,

    It is probably that the un-typed version of the ubin output of debug doesn't show leading zeroes to pad out a byte.

    Try:

    debug ("flipped ",ubin_byte(val))

    to format the displayed output as a full byte.

  • Cluso99Cluso99 Posts: 18,069

    @"Greg LaPolla" said:
    It's missing a 0 at the beginning of the second debug. the value after the !val should be %01111111

    There is an implied 0 at the beginning.
    %1111111 = %01111111

  • cgraceycgracey Posts: 14,133

    var!

    That will flip all bits.

Sign In or Register to comment.