Using ~ (not) operator in array index
![Zoot](https://forums.parallax.com/uploads/userpics/559/nW4UO23UEIWZ4.png)
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.
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?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
When the going gets weird, the weird turn pro. -- HST
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
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 -->
~%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:
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
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 -->
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
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
·
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?
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