Left shift

From BeebWiki
Jump to: navigation, search

Left shift, <<, is a bitwise operator to shift a binary value to the left.

<<
Availability BASIC V
Syntax BASIC V <num-var> = <numeric> << <numeric>
Token (hex) BASIC V 3C 3C (operator)
Description BASIC V Returns the first operand, with the bits of its binary representation shifted toward the most significant end by the number of places specified in the second operand, and zeroes shifted in at the least significant end.
Associated keywords >>, >>>, AND, DIV, EOR, NOT, OR

Description

<< accepts two integer values, and returns the first value left-shifted by the number of binary places given in the second operand. For instance, in the statement

PRINT ~&87654321 << 4

the binary digits of &87654321, 10000111011001010100001100100001, are moved four places to the left in the register. In this case, the four most significant bits (1000) are discarded, and the units' place (bit 0) becomes the sixteens' place (bit 4) of the returned value. As it is a logical shift, zeroes are shifted in at the bottom, so that bits 0 to 3 of the result are all 0s. BASIC performs the shift in a single instruction, but if we consider the shift as a step-by-step calculation, it may be represented as:

Operand    10000111011001010100001100100001
                                         /
1st shift  00001110110010101000011001000010 ←
                                        /
2nd shift  00011101100101010000110010000100 ←
                                       /
3rd shift  00111011001010100001100100001000 ←
                                      /
4th shift  01110110010101000011001000010000 ←

The output from the statement is:

>PRINT ~&87654321 << 4
  76543210

Notes

The operation is mathematically equivalent to unsigned integer multiplication by the respective power of two. By contrast the expression A% = Q% * 2^N% causes an error if the signed integer A% would overflow and although A% = A%+A% is an unsigned left shift on Acorn/Wilson BASICs, it causes an overflow error on Russell BASICs. << also takes the two's complement of a negative shift distance.

On ARM processors, a shift distance between 32 and 255 inclusive returns zero; any larger, or negative, distance N% has the same effect as N% AND 255. BASIC on other architectures may give different results when the second operand is more than 31 or less than 0.

As a Group 5 operator << does not associate with other Group 5 operators, so parentheses must be used when an operand has such an operator on either side. For example:

   IF (dword% << 24) = &1000000 THEN ...

See = for more details.

Compatibility

The following routines will do a binary left shift, the equivalent of << 1, and a binary left rotate on any BBC BASIC:

   REM Shift Left:   abcdefgh -> bcdefgh0
   DEFFNshl(A%)=((A%AND&3FFFFFFF)*2)OR(((A%AND&40000000)>0)AND&80000000)
   :
   REM Rotate Left:  abcdefgh -> bcdefgha
   DEFFNrol(A%)=((A%AND&3FFFFFFF)*2)OR(((A%AND&40000000)<>0)AND&80000000)OR-(A%<0)
   :

beardo 02:54, 3 September 2011 (UTC) Jgharston (talk) 16:18, 15 January 2024 (CET)