Shop OBEX P1 Docs P2 Docs Learn Events
Using ~ (not) operator in array index — Parallax Forums

Using ~ (not) operator in array index

ZootZoot Posts: 2,227
edited 2006-08-14 18:52 in BASIC Stamp
I've got a pretty complex program that I decided to re-work over the weekend and I hit a problem.

I want to access some arrays (mostly for two motors and two sonars) using a single BIT and the not operator. However, using the ~ operator in the array index appears to crash. I tested this using simple debug program, so I am wondering if I am off or if I have to do a workaround, or what.

somearr   VAR   Byte(2)
somebit    VAR   Bit

Reset:
somearr(0) = "A"
somearr(1) = "B"

Main:
somebit = 0
DEBUG somearr(somebit), CR    'gives me "A"
DEBUG somearr(~somebit), CR  'I would like "B" but I get a crash
somebit = 1
DEBUG somearr(somebit), CR    'gives me "B"
DEBUG somearr(~somebit), CR  'I would like "A" but I get a crash
GOTO Main
END




The workaround I found is below (which works), but boy, I don't see why the above shouldn't work since I can do other operations to get array indices?

somearr   VAR   Byte(2)
somebit    VAR   Bit
ix   VAR   Bit

Reset:
somearr(0) = "A"
somearr(1) = "B"

Main:
somebit = 0
DEBUG somearr(somebit), CR    'gives me "A"
ix = ~somebit
DEBUG somearr(ix), CR  'give me "B"
somebit = 1
DEBUG somearr(somebit), CR    'gives me "B"
ix = ~somebit
DEBUG somearr(ix), CR  'gives me "A"
GOTO Main
END


▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
When the going gets weird, the weird turn pro. -- HST

Comments

  • Bruce BatesBruce Bates Posts: 3,045
    edited 2006-08-14 15:38
    Zoot -

    What you're trying to do with this, is beyond me:
    DEBUG somearr(~somebit), CR 'I would like "A" but I get a crash

    somebit, somebit+1 and somebit-1 all make sense, but not much else. You are aware that ~ is the negation operator?

    A var byte
    B var byte

    A = 1
    B = ~A
    B = -1

    How do you define a NEGATIVE binary BIT? I know of NO definition. In 10 years I've never seen a Stamp "crash", what does it do when it "crashes?

    Regards,

    Bruce Bates

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    <!--StartFragment -->
  • ZootZoot Posts: 2,227
    edited 2006-08-14 17:39
    The Basic Stamp manual defines ~ as "Inverse" or "Bitwise NOT" -- it does 1s complement (or inverts) on the bits, e.g.

    ~%1100 eq. %0011

    It doesn't make it negative. Unless I'm really missing something. I use the ~ operator all the time, it just doesn't seem to work when applied as an expression to reference an array element. Here's a more real world example:

    motor VAR Word(2)
    sonar  VAR Word(2)
    servopos VAR Word(2)
    turndir  VAR  Bit
    idx VAR Nib
    work VAR Word
    left CON 0
    right CON 1
    
    Main:
    
    '.....do lots of sensor gathering and parsing, etc......
    '....then do something useful:
    IF ( sonar(left) > sonar(right) THEN
       turndir = 0
    ELSE
      turndir = 1
    ENDIF
    
    motor(turndir) = motor(turndir) - 1
    motor(~turndir) = motor(~turndir) + 1     ' except this doesn't work and crashes a debugger or gives me garbage; see earlier examples
    
    work = 1750 - ( turndir * 500 )
    FOR idx = left TO right
       servopos(idx) = work
    NEXT
    
    '.........etc
    
    GOTO Main
    
    



    This approach (for me, anyway) is really handy as I propagate the direction bit through a lot of state machines, servos, etc. Again, I can use the lower example in my first post as a workaround, but it's clunkier than just using the ~ on my bit.

    As far as the "crash" -- I've never seen it either -- it's the only way to describe what happens. When I compile and run my very first example at top, the debug screen goes wacky -- basically printing only characters up to the "~somebit" then it blanks and flashes a lot. When I compile the workaround it's just dandy.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2006-08-14 18:33
    Zoot -

    Sorry, my error. I guess you'll have to ask Jeff Martin from Parallax.

    Very strange "crash" results. Jeff will want to know about that too, although that's a compile time error, not a run time error.

    Regards,

    Bruce Bates

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    <!--StartFragment -->
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2006-08-14 18:36
    The usual trip up with ~ is to realize that the result is a word, not a bit. So in the context of
    motor(~turndir)
    if turndir=0, then ~turndir=65535
    and if turndir=1, then ~turndir=65534

    If you have a bit variable,

    turndir VAR bit
    turndir = ~turndir
    that will work fine, because it picks off bit0 for the result, even though the intermediate calculation of ~turndir is a word. But that doesn't happen when you use it as an array index.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • BeanBean Posts: 8,129
    edited 2006-08-14 18:40
    Could you not use "somearr(1-somebit)" instead of "somearr(~somebit)"

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Cheap 4-digit LED display with driver IC·www.hc4led.com

    Low power SD Data Logger www.sddatalogger.com

    "You're braver than you believe, stronger than you seem, and smarter than you think" Christopher Robin to Pooh
    ·
  • ZootZoot Posts: 2,227
    edited 2006-08-14 18:52
    Well, at least that explains it. I thought I was losing my mind. It also explains why my ~ operations work as I expected them to with Bytes (the signed bit would not be relevant). It probably also explains my debugger "crash" -- even though RAM should have rolled over, I'm not sure what happens when you actually tell the Stamp to look for somevar(65535)

    Ahhhh...I just looked it up....the documentation says all the unary operators work in 16 bits. I just never thought about correctly. Thanks so much for saving me days of hair-pulling.

    In terms of elegant (and easy to read) workaround, I would think something like this would work?

    turndir = 0
    motor(~turndir & $0001) = someval
    motor(turndir) = someval
    
    



    Bean -- I just saw your post while previewing this one. That would work in this case, and is much simpler. THANK YOU. I see only trees, no forest. Although I have few state machines I've been trying out that use two bits (not 1), and I think I'd have to use an approach similar to the above.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST
Sign In or Register to comment.