User Manual

MCP Series
Brushed DC Motor Controllers
MCP Series User Manual
136
Shift Left (<<)
The Shift Left operator shifts all the bits of a value to the left by a specied amount. Shifting left
is the same as multiplying the value by 2 to the nth power. Bits shifted off the left end are lost.
Zeros are added to the right for vacant bits. The example program will display the value of time
before and after shifting left by 4. The results will be displayed in binary:
time var byte
serout sout, i9600,[bin time]
time = time << 4
serout sout, i9600,[bin time]
Important: The sign bit is not preserved so this function should not be used with signed
numbers.
Shift Right (>>)
The Shift Right operator shifts all the bits of a value to the right by a specied amount. Shifting
right is the same as dividing the value by 2 to the nth power. Bits shifted off the right end are
lost. Zeros are added to the left for vacant bits. The example program will display the value of
time before and after shifting right by 4. The results will be displayed in binary:
time var byte
serout sout, i9600,[bin time]
time = time >> 4
serout sout, i9600,[bin time]
Important: The sign bit is not preserved so this function should not be used with signed
numbers.
AND (&)
The AND (&) function is a binary operator. It sets the result to 1 if both bits are 1’s or 0 if either
or both bits are 0’s.
1 & 1 = 1
1 & 0 = 0
0 & 1 = 0
0 & 0 = 0
Value1 0 1 0 1 1 1 0 1
Value2 1 0 0 1 0 0 1 1
Result 0 0 0 1 0 0 0 1
One useful function for AND is to “mask” certain bits of a number. For example, if we are
interested only in the low 4 bits of a number, and wanted to ignore the high 4 bits, we could AND
(&) the number with 00001111 as shown here: