Difference between revisions of "Arithmetic right shift"
m (1 revision) |
m (Slight formatting change.) |
||
(2 intermediate revisions by one other user not shown) | |||
Line 1: | Line 1: | ||
− | Arithmetic right shift, ''' | + | [[Category:BASIC keywords]] |
+ | Arithmetic right shift, '''>>''', is a ''bitwise operator'' to shift a | ||
+ | binary value to the right, with sign extension. | ||
{| class="wikitable" | {| class="wikitable" | ||
− | |+ | + | |+ >> |
| Availability | | Availability | ||
| colspan="2" | BASIC V | | colspan="2" | BASIC V | ||
Line 8: | Line 10: | ||
| [[BASIC metasyntax|Syntax]] | | [[BASIC metasyntax|Syntax]] | ||
| BASIC V | | BASIC V | ||
− | | | + | | <num-var> = <numeric> <code>>></code> <numeric> |
|- style="vertical-align:top" | |- style="vertical-align:top" | ||
| Token (hex) | | Token (hex) | ||
Line 19: | Line 21: | ||
|- style="vertical-align:top" | |- style="vertical-align:top" | ||
| Associated keywords | | Associated keywords | ||
− | | colspan="2" | <code>[[Left shift| | + | | colspan="2" | <code>[[Left shift|<<]]</code>, <code>[[Logical right shift|>>>]]</code>, <code>[[AND]]</code>, <code>[[DIV]]</code>, <code>[[EOR]]</code>, <code>[[NOT]]</code>, <code>[[OR]]</code> |
|} | |} | ||
== Description == | == Description == | ||
− | <code> | + | <code>>></code> accepts two integer values, and returns the first value |
− | PRINT ~&87654321 | + | ''right-shifted'' by the number of binary places given in the second |
− | the binary digits of &87654321, 10000111011001010100001100100001, are moved four places to the right in the register. | + | 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 an | ||
+ | ''arithmetic shift'', the most significant bit is duplicated four times, so | ||
+ | that bits 27 to 31 of the result are the same - all 1s in this case. | ||
+ | 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 | Operand 10000111011001010100001100100001 | ||
|\ | |\ | ||
Line 37: | Line 49: | ||
4th shift 11111000011101100101010000110010 → | 4th shift 11111000011101100101010000110010 → | ||
The output from the statement is: | The output from the statement is: | ||
− | + | >PRINT ~&87654321 >> 4 | |
F8765432 | F8765432 | ||
− | == Notes == | + | ==Notes== |
− | + | The operation differs significantly from integer division by the respective | |
− | The operation differs significantly from integer division by the respective power of two. | + | power of two. The expression <code>Q% = A% DIV 2^N%</code> causes an error |
+ | when <code>N% >= 31</code>, and otherwise rounds toward zero whereas | ||
+ | <code>Q% = A% >> N%</code> rounds toward negative infinity. <code>>></code> | ||
+ | also takes the two's complement of a negative shift distance. | ||
− | On ARM processors, a shift distance between 31 and 255 inclusive copies bit 31 of the first operand to all bits of the result, and any larger, or negative, distance <code>N%</code> has the same effect as <code>N% AND 255</CODE>. | + | On ARM processors, a shift distance between 31 and 255 inclusive copies bit |
+ | 31 of the first operand to all bits of the result, and 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 results when the | ||
+ | second operand is more than 31 or less than 0. | ||
− | As a Group 5 operator <code> | + | As a Group 5 operator <code>>></code> does not associate with other Group 5 |
− | IF (dword% | + | operators, so parentheses must be used when an operand has such an operator |
+ | on either side. For example: | ||
+ | IF (dword% >> 24) = 1 THEN ... | ||
See <code>[[=]]</code> for more details. | See <code>[[=]]</code> for more details. | ||
− | A triple greater-than sign makes the ''logical right shift'' operator, <code>[[Logical right shift| | + | A triple greater-than sign makes the ''logical right shift'' operator, |
+ | <code>[[Logical right shift|>>>]]</code>, which keeps the vacant upper bits | ||
+ | at zero. | ||
− | + | [[User:Beardo|beardo]] 02:54, 3 September 2011 (UTC) | |
− |
Latest revision as of 16:51, 12 November 2017
Arithmetic right shift, >>, is a bitwise operator to shift a binary value to the right, with sign extension.
Availability | BASIC V | |
Syntax | BASIC V | <num-var> = <numeric> >> <numeric>
|
Token (hex) | BASIC V | 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 the most significant bit copied into the vacant bits. |
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 an arithmetic shift, the most significant bit is duplicated four times, so that bits 27 to 31 of the result are the same - all 1s in this case. 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 11000011101100101010000110010000 → | \ 2nd shift 11100001110110010101000011001000 → | \ 3rd shift 11110000111011001010100001100100 → | \ 4th shift 11111000011101100101010000110010 →
The output from the statement is:
>PRINT ~&87654321 >> 4 F8765432
Notes
The operation differs significantly from integer division by the respective
power of two. The expression Q% = A% DIV 2^N%
causes an error
when N% >= 31
, and otherwise rounds toward zero whereas
Q% = A% >> N%
rounds toward negative infinity. >>
also takes the two's complement of a negative shift distance.
On ARM processors, a shift distance between 31 and 255 inclusive copies bit
31 of the first operand to all bits of the result, and 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 triple greater-than sign makes the logical right shift operator,
>>>
, which keeps the vacant upper bits
at zero.
beardo 02:54, 3 September 2011 (UTC)