Merge pull request #6083 from ethereum/docs-shift-clarify

[DOCS] Clarify shift operator
This commit is contained in:
chriseth 2019-03-13 11:32:58 +01:00 committed by GitHub
commit 9ccd5dfec3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^