View Full Version : Whats with (no) negative numbers in the BS1?

Amnoid

02-24-2005, 05:48 AM

I did some basic math to compare two analog inputs on a BS1. The manual sais that I can see negative nubers (i.e. 10- 20= -10). But instead I get something near 65533 (similar to when an 8bit variable reaches the end of its number stack·and starts back at 0).... I am using word variables (W0 - W1), but still don't see any NEGATIVE numbers. I was able to work around this, but I would still like to use simple math. Any hints?

Thanks

Robert Schwartz

02-24-2005, 07:15 AM

The Basic Stamps used unsigned numbers. The way this works is that signed numbers have an extra bit in them that determines if they arepositive or negative. Parallax figured that more people would want to use the larger numbers than negative numbers, so they made all of the data types unsigned, meaning that they use that bit to store larger numbers.

Amnoid

02-24-2005, 01:24 PM

Here is what it sais in the BS help file:

- Subtraction

The Subtraction operator (-) subtracts variables and/or constants, returning a 16-bit result. Works exactly as you would expect with unsigned integers from 0 to 65535. >>>>> If the result is negative, it will be correctly expressed as a signed 16-bit number. <<<<<

So where is the mistake? Is there a way to change a number to signed?

Bushman

02-24-2005, 01:45 PM

You can use ABS to get the absolute difference between the two.

compare VAR Word

value1 VAR Word

Value2 VAR Word

compare = ABS (value1 - value2)

Though im not sure if this helps you or not sorry

Jon Williams

02-24-2005, 11:12 PM

All BASIC Stamps store negative values in two's compliment format -- this is required in an integer only system. After a subtraction you can look at bit15 of the result; it that bit is set the value is negative.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔

Jon Williams

Applications Engineer, Parallax

Dallas, TX· USA

Amnoid

02-25-2005, 05:09 AM

Thanks for the tips. I solved it by checking the distance between 0 and the value- positive numbers is obvious, "negative" numbers simply count down from the last possible number in the 16bit scale. Weird- but it works.

Thanks