As revealed by the title, in JavaScript there is a specific operator >>>
. For example, in JavaScript we will have the following result:
(-1000) >>> 3 = 536870787 (-1000) >> 3 = -125 1000 >>> 3 = 125 1000 >> 3 = 125
So is there a certain method or operator representing this >>>
?
Division. To divide a number, a binary shift moves all the digits in the binary number along to the right and fills the gaps after the shift with 0: to divide by two, all digits shift one place to the right. to divide by four, all digits shift two places to the right.
They are bit shift operator which exists in many mainstream programming languages, << is the left shift and >> is the right shift, they can be demonstrated as the following table, assume an integer only take 1 byte in memory.
In Python >> is called right shift operator. It is a bitwise operator. It requires a bitwise representation of object as first operand. Bits are shifted to right by number of bits stipulated by second operand. Leading bits as towards left as a result of shifting are set to 0.
Actually, Python only has arithmetic right shift; logical right shift would not make sense in the context of Python's unbounded integer type. For logical right shift to be defined, you need to specify the number of bits used to represent an integer.
There isn't a built-in operator for this, but you can easily simulate the >>>
yourself:
>>> def rshift(val, n): return val>>n if val >= 0 else (val+0x100000000)>>n ... >>> rshift(-1000, 3) 536870787 >>> rshift(1000, 3) 125
The following alternative implementation removes the need for the if
:
>>> def rshift(val, n): return (val % 0x100000000) >> n
No, there isn't. The right shift in python is arithmetical.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With