Unary Minus Supported?

Armp

Senior Member
As a result of a typing mishit (!) while entering code I found that

w1 = -w1

is accepted by the simulator, and gives the expected 2s Comp result. ie w1=65535, then -w1 = 1.

w1 = -1234 is also accepted.

Can't find this in the docs, and in fact I think I saw somewhere that it was not allowed?
 
Last edited:

hippy

Ex-Staff (retired)
Not sure if it's in Manual 2 or not and I'll make a note of it.

It was a very long time ago when unary minus wasn't accepted and 'w1 = 0-w1' had to be used, and I think that may have just been a compiler issue.
 
Last edited:

nick12ab

Senior Member
Multiplying something by -1 throws a syntax error but 'w1 = 0 -x' takes up one byte more program memory than 'w1 = -x'
 

hippy

Ex-Staff (retired)
Unary operators have to appear at the start of the assignment, so to multiply by -1 ...

Let w1 = -1 * w2
 

crowland

Member
I've used unary minus in an Abs function:
Code:
Abs:
    if w0 >= $8000 then
        w0 = -w0
    endif
    return
Useful for handling parameters from devices such as electronic compasses that return signed integers.
Chris
 
Top