mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Merge pull request #6083 from ethereum/docs-shift-clarify
[DOCS] Clarify shift operator
This commit is contained in:
commit
9ccd5dfec3
@ -59,15 +59,16 @@ This means that, for example ``~int256(0) == int256(-1)``.
|
||||
Shifts
|
||||
^^^^^^
|
||||
|
||||
The result of a shift operation has the type of the left operand. The
|
||||
expression ``x << y`` is equivalent to ``x * 2**y``, and, for positive integers,
|
||||
``x >> y`` is equivalent to ``x / 2**y``. For negative ``x``, ``x >> y``
|
||||
is equivalent to dividing by a power of ``2`` while rounding down (towards negative infinity).
|
||||
Shifting by a negative amount throws a runtime exception.
|
||||
The result of a shift operation has the type of the left operand, truncating the result to match the type.
|
||||
|
||||
- For positive and negative ``x`` values, ``x << y`` is equivalent to ``x * 2**y``.
|
||||
- For positive ``x`` values, ``x >> y`` is equivalent to ``x / 2**y``.
|
||||
- For negative ``x`` values, ``x >> y`` is equivalent to ``(x + 1) / 2**y - 1`` (which is the same as dividing ``x`` by ``2**y`` while rounding down towards negative infinity).
|
||||
- In all cases, shifting by a negative ``y`` throws a runtime exception.
|
||||
|
||||
.. warning::
|
||||
Before version ``0.5.0`` a right shift ``x >> y`` for negative ``x`` was equivalent to ``x / 2**y``,
|
||||
i.e. right shifts used rounding towards zero instead of rounding towards negative infinity.
|
||||
i.e., right shifts used rounding up (towards zero) instead of rounding down (towards negative infinity).
|
||||
|
||||
Addition, Subtraction and Multiplication
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
Loading…
Reference in New Issue
Block a user