Specifications

BASIC Stamp II
Page 274 • BASIC Stamp Programming Manual 1.8 • Parallax, Inc.
Internal Workings and Potential Bugs
Internally, the BS2 defines “false” as 0 and “true” as any value other
than 0. Consider the following instructions:
flag var bit
flag = 1
IF flag THEN isTrue
debug "false"
stop
isTrue:
debug "true"
stop
Since flag is 1, If...Then would evaluate it as true and print the message
“true” on the screen. Suppose you changed the If...Then instruction to
read “IF NOT flag THEN isTrue.” That would also evaluate as true.
Whoa! Isn’t NOT 1 the same thing as 0? No, at least not in the 16-bit
world of the BS2.
Internally, the BS2 sees a bit variable containing 1 as the 16-bit number
%0000000000000001. So it sees the NOT of that as %1111111111111110.
Since any non-zero number is regarded as true, NOT 1 is true. Strange
but true.
The easiest way to avoid the kinds of problems this might cause is to
always use a conditional operator with If...Then. Change the example
above to read IF flag=1 THEN isTrue. The result of the comparison
will follow If...Then rules. And the logical operators will work as they
should; IF NOT flag=1 THEN isTrue will correctly evaluate to false
when flag contains 1.
This also means that you should only use the named logic operators
NOT, AND, OR, and XOR with If...Then. These operators format their
results correctly for If...Then instructions. The other logical operators,
represented by symbols ~ & | and ^ do not.
Demo Program
The program below generates a series of 16-bit random numbers and
tests each to determine whether they’re divisible by 3. (A number is