BASIC stamp manual v2.2

BASIC Stamp Architecture – Math and Operators
Page 104 BASIC Stamp Syntax and Reference Manual 2.2 www.parallax.com
All BS2 models can interpret twos complement negative numbers correctly
in DEBUG and SEROUT instructions using formatters like SDEC (for
signed decimal). In calculations, however, it assumes that all values are
positive. This yields correct results with two’s complement negative
numbers for addition, subtraction, and multiplication, but not for division.
The standard operators we just discussed: +, - ,* and / all work on two
values; as in 1 + 3 or 26 * 144. The values that operators process are
referred to as arguments. So we say that the add, subtract, multiply and
divide operators take two arguments.
Operators that take two arguments are called “binary” operators, and
those that take only one argument are called “unary” operators. Please
note that the term “binary operator” has nothing to do with binary
numbers; it’s just an inconvenient coincidence that the same word,
meaning ‘involving two things’ is used in both cases.
The minus sign (-) is a bit of a hybrid. It can be used as a binary operator,
as in 8-2 = 6, or it can be used as a unary operator to represent negative
numbers, such as -4.
Unary operators take precedence over binary operators; the unary
operation is always performed first. For example, on all BS2 models, SQR
is the unary operator for square root. In the expression 10 - SQR 16, the
BASIC Stamp first takes the square root of 16, then subtracts it from 10.
Most of the descriptions that follow say something like “computes (some
function) of a 16-bit value.” This does not mean that the operator does not
work on smaller byte or nibble values, but rather that the computation is
done in a 16-bit workspace. If the value is smaller than 16 bits, the BASIC
Stamp pads it with leading 0s to make a 16-bit value. If the 16-bit result of
a calculation is to be packed into a smaller variable, the higher-order bits
are discarded (truncated).
Keep this in mind, especially when you are working with two’s
complement negative numbers, or moving values from a larger variable to
a smaller one. For example, look at what happens when you move a two’s
complement negative number into a byte (rather than a word):
U
NARY AND BINARY OPERATORS.
N
OTES ABOUT THE 16-BIT WORKSPACE.
All
2