Difference between revisions of "Logical right shift"

From BeebWiki
Jump to: navigation, search
(Added universal routines.)
m (Fixed broken formatting.)
 
Line 21: Line 21:
 
|- style="vertical-align:top"
 
|- style="vertical-align:top"
 
| Associated keywords
 
| Associated keywords
| colspan="2" | <code>[[Left shift|<<]]</code>, <code>[[Arithmetic right shift|>>]]</code>, <code>[[AND]]</code>, <code>[[DIV]]</code>, <code>[[EOR]]</code&gt;, <code>[[NOT]]</code>, <code>[[OR]]</code>
+
| colspan="2" | <code>[[Left shift|<<]]</code>, <code>[[Arithmetic right shift|>>]]</code>, <code>[[AND]]</code>, <code>[[DIV]]</code>, <code>[[EOR]]</code>, <code>[[NOT]]</code>, <code>[[OR]]</code>
 
|}
 
|}
  
 
== Description ==
 
== Description ==
 
 
<code>>>></code> accepts two integer values, and returns the first value
 
<code>>>></code> accepts two integer values, and returns the first value
 
''right-shifted'' by the number of binary places given in the second
 
''right-shifted'' by the number of binary places given in the second
 
operand.  For instance, in the statement
 
operand.  For instance, in the statement
  
PRINT ~&87654321 >>> 4
+
    PRINT ~&87654321 >>> 4
  
 
the binary digits of &87654321, 10000111011001010100001100100001, are moved
 
the binary digits of &87654321, 10000111011001010100001100100001, are moved
four places to the right in the register. In this case, the four least
+
four places to the right in the register. In this case, the four least
 
significant bits (0001) are discarded, and the sixteens' place (bit 4)
 
significant bits (0001) are discarded, and the sixteens' place (bit 4)
becomes the units' place (bit 0) of the returned value. As it is a
+
becomes the units' place (bit 0) of the returned value. As it is a
 
''logical shift'', zeroes are shifted in at the top, so that bits 28 to 31
 
''logical shift'', zeroes are shifted in at the top, so that bits 28 to 31
of the result are all 0s. BASIC performs the shift in a single instruction,
+
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
 
but if we consider the shift as a step-by-step calculation, it may be
 
represented as:
 
represented as:
  
Operand    10000111011001010100001100100001
+
    Operand    10000111011001010100001100100001
            \
 
1st shift  01000011101100101010000110010000 →
 
              \
 
2nd shift  00100001110110010101000011001000 →
 
              \
 
3rd shift  00010000111011001010100001100100 →
 
 
                 \
 
                 \
  4th shift  00001000011101100101010000110010 →
+
    1st shift  01000011101100101010000110010000 →
 +
                \
 +
    2nd shift  00100001110110010101000011001000 →
 +
                  \
 +
    3rd shift 00010000111011001010100001100100 →
 +
                  \
 +
    4th shift  00001000011101100101010000110010
  
 
The output from the statement is:
 
The output from the statement is:
  
>PRINT ~&87654321 >>> 4
+
    >PRINT ~&87654321 >>> 4
    8765432
+
      8765432
  
 
== Notes ==
 
== Notes ==
 
 
<code>>>></code> takes the two's complement of a negative shift distance.
 
<code>>>></code> takes the two's complement of a negative shift distance.
  
 
On ARM processors, a shift distance between 32 and 255 inclusive returns
 
On ARM processors, a shift distance between 32 and 255 inclusive returns
 
zero; any larger, or negative, distance <code>N%</code> has the same effect
 
zero; any larger, or negative, distance <code>N%</code> has the same effect
as <code>N% AND 255</CODE>. BASIC on other architectures may give different
+
as <code>N% AND 255</CODE>. BASIC on other architectures may give different
 
results when the second operand is more than 31 or less than 0.
 
results when the second operand is more than 31 or less than 0.
  
 
As a Group 5 operator <code>>>></code> does not associate with other Group 5
 
As a Group 5 operator <code>>>></code> does not associate with other Group 5
 
operators, so parentheses must be used when an operand has such an operator
 
operators, so parentheses must be used when an operand has such an operator
on either side. For example:
+
on either side. For example:
  
IF (dword% >>> 24) = 1 THEN ...
+
    IF (dword% >>> 24) = 1 THEN ...
  
 
See <code>[[=]]</code> for more details.
 
See <code>[[=]]</code> for more details.

Latest revision as of 17:26, 15 January 2024

Logical right shift, >>>, is a bitwise operator to shift a binary value to the right, without sign extension.

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

Description

>>> accepts two integer values, and returns the first value right-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 right in the register. In this case, the four least significant bits (0001) are discarded, and the sixteens' place (bit 4) becomes the units' place (bit 0) of the returned value. As it is a logical shift, zeroes are shifted in at the top, so that bits 28 to 31 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  01000011101100101010000110010000 →
                \
   2nd shift  00100001110110010101000011001000 →
                 \
   3rd shift  00010000111011001010100001100100 →
                  \
   4th shift  00001000011101100101010000110010 →

The output from the statement is:

   >PRINT ~&87654321 >>> 4
      8765432

Notes

>>> 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) = 1 THEN ...

See = for more details.

A double greater-than sign makes the arithmetic right shift operator, >>, which duplicates the most significant bit to perform sign extension.

Compatibility

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

   REM Shift Right:  abcdefgh -> 0abcdefg
   DEFFNshr(A%)=((A%AND&7FFFFFFF)DIV2)OR((A%<0)AND&40000000)
   :
   REM Rotate Right: abcdefgh -> habcdefg
   DEFFNror(A%)=((A%AND&7FFFFFFF)DIV2)OR((A%<0)AND&40000000)OR(((A%AND1)>0)AND&80000000)
   :

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