Unary Operators - Parallax BASIC Stamp 2e Programming Manual

Table of Contents

Advertisement

4: BASIC Stamp Architecture – Math and Operators
2
2
e
U
O
.
NARY
PERATORS
2
2
1
e
Table 4.5: Unary Operators. Note:
the BS1 only supports the negative
(-) unary operator.
work on smaller byte or nibble values. It just means 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):
2
2
Value
VAR
p
sx
Value = - 99
DEBUG SDEC ? Value
How did -99 become 157? Let's look at the bits: 99 is %01100011 binary.
When the BASIC Stamp negates 99, it converts the number to 16 bits
%0000000001100011,
%1111111110011101. Since we've asked for the result to be placed in an 8-
bit (byte) variable, the upper eight bits are truncated and the lower eight
bits stored in the byte: %10011101.
Now for the second half of the story. DEBUG's SDEC modifier (on the BS2,
BS2e, BS2sx and BS2p) expects a 16-bit, two's complement value, but
we've only given it a byte to work with. As usual, it creates a 16-bit value
by padding the leading eight bits with 0s: %0000000010011101. And what's
that in signed decimal? 157.
Table 4.5 lists the available Unary Operators. Note: the BS1 only supports
2
2
negative (-).
p
sx
Operator
ABS
COS
DCD
~
-
NCD
SIN
SQR
BYTE
and
then
Description
Returns absolute value
Returns cosine in two's compliment
binary radians
n
2
-power decoder
Inverse
Negative
Priority encoder of a 16-bit value
Returns sine in two's compliment
binary radians
Returns square root of value
BASIC Stamp Programming Manual 2.0c • www.parallaxinc.com • Page 63
' Show signed decimal result (157).
takes
the
two's
Supported By:
All except BS1
All except BS1
All except BS1
All except BS1
All
All except BS1
All except BS1
All except BS1
complement,

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the BASIC Stamp 2e and is the answer not in the manual?

Table of Contents