BASIC Stamp Architecture – DIG, <<, >>
65535+1 MAX 65535 will result in 0 because of the way fixed-size integers
wrap around.
SYMBOL
Value1 = W0
SYMBOL
Value2 = W1
FOR Value1= 0 TO 100 STEP 10
Value2 = Value1 MAX 50
DEBUG Value2
NEXT
-- or --
Value1
VAR
WORD
FOR Value1= 0 TO 100 STEP 10
DEBUG ? Value1 MAX 50
NEXT
The Digit operator (DIG) returns the specified decimal digit of a 16-bit
positive value. Digits are numbered from 0 (the rightmost digit) to 4 (the
leftmost digit of a 16-bit number; 0 to 65535). Example:
Value
VAR
WORD
Idx
VAR
BYTE
Value = 9742
DEBUG ? Value DIG 2
FOR Idx = 0 TO 4
DEBUG ? Value DIG Idx
NEXT
The Shift Left operator (<<) shifts the bits of a value to the left a specified
number of places. Bits shifted off the left end of a number are lost; bits
shifted into the right end of the number are 0s. Shifting the bits of a value
left n number of times has the same effect as multiplying that number by 2
th
to the n
power. For instance 100 << 3 (shift the bits of the decimal number
100 left three places) is equivalent to 100 * 2
Value
VAR
WORD
Idx
VAR
BYTE
Value = %1111111111111111
FOR Idx = 1 TO 16
DEBUG BIN ? Value << Idx
NEXT
The Shift Right operator (>>) shifts the bits of a variable to the right a
specified number of places. Bits shifted off the right end of a number are
lost; bits shifted into the left end of the number are 0s. Shifting the bits of a
Page 72 • BASIC Stamp Programming Manual 2.0b • www.parallaxinc.com
' Walk value of Value1 from 0 to 100.
' Use MAX to clamp at 50.
' Show "clamped" value
' Walk value of Value1 from 0 to 100.
' Show Value1, but use MAX to clamp at 50.
' Show digit 2 (7)
' Show digits 0 through 4 of 9742.
3
. Here's an example:
' Repeat with Idx = 1 to 16.
' Shift Value left Idx places.
1
2
2
2
2
e
p
sx
D
: DIG
IGIT
2
2
2
2
e
sx
p
S
L
: <<
HIFT
EFT
2
2
2
2
e
p
sx
S
R
: >>
HIFT
IGHT
2
2
2
2
e
sx
p
Need help?
Do you have a question about the BASIC Stamp 2e and is the answer not in the manual?