Appendix A. CRBasic Programming Instructions
<=
Less than or equal
to
A.7.3 Bitwise Operations
Bitwise shift operators (<< and >>) allow CRBasic to manipulate the position of
bits within a variable declared As Long (integer). Following are example
expressions and expected results:
• &B00000001 << 1 produces &B00000010 (decimal 2)
• &B00000010 << 1 produces &B00000100 (decimal 4)
• &B11000011 << 1 produces &B10000110 (decimal 134)
• &B00000011 << 2 produces &B00001100 (decimal 12)
• &B00001100 >> 2 produces &B00000011 (decimal 3)
The result of these operators is the value of the left-hand operand with all of its
bits moved by the specified number of positions. The resulting "holes" are filled
with zeros.
Smart sensors, or a communication protocol, may output data that are compressed
into integers that are composites of "packed" fields. This type of data
compression is a tactic to conserve memory and communication bandwidth.
Following is an example of data compressed into an eight-byte integer:
A packed integer that is stored in variable input_val will be unpacked into
three integers individually stored in value_1, value_2, and value_3. In the
packed integer, the information that is unpacked into value_1 is stored in bits
7 and 6, value_2 is unpacked from bits 5 and 4, and value_3 from bits 3, 2, 1,
and 0. The CRBasic code to do this unpacking routine is shown in CRBasic
example Using Bit-Shift Operators
(p. 564).
With unsigned integers, shifting left is equivalent to multiplying by two. Shifting
right is equivalent to dividing by two.
The operators follow:
<<
Bitwise left shift
Syntax
Variable = Numeric Expression << Amount
>>
Bitwise right shift
Syntax
Variable = Numeric Expression >> Amount
&
Bitwise AND assignment — performs a bitwise AND of a variable with an
expression and assigns the result back to the variable.
564