Some languages like Java, Verilog have both bitwise logical (<<, >>) and arithmetic shift (<<<, >>>) operators.
For unsigned values, logical and arithmetic shifts have identical operation. Say if 8'b11000101 is binary representation of 8-bit unsigned number 197, then
8'b11000101 >> 2 => 8'b00110001
8'b11000101 >>> 2 => 8'b00110001
8'b11000101 << 2 => 8'b00010100
8'b11000101 <<< 2 => 8'b00010100
For signed values, only the arithmetic and logical left shift operations are identical but arithmetic right shift leads to sign extension. Say if 8'b11000101 is binary representation of 8-bit signed number -59, then
8'b11000101 >> 2 => 8'b00110001
8'b11000101 >>> 2 => 8'b11110001
8'b11000101 << 2 => 8'b00010100
8'b11000101 <<< 2 => 8'b00010100
Python only has logical shift operators but no arithmetic shift operators. So how to achieve arithmetic right shift in python for signed and unsigned values ?
Python actually only has arithmetic shift operators: Left shifting by n is exactly the same as multiplying by 2 to the power n for both positive an negative values. Right shifting by n depends on whether the value being shifted is negative or not.
The Python bitwise right-shift operator x >> n shifts the binary representation of integer x by n positions to the right. It inserts a 0 bit on the left and removes the right-most bit.
In unsigned right shift, the rightmost bit is discarded, and the leftmost bit is filled with 0. The signed right bit operator and the unsigned right bit operator produce the same result for positive numbers but different results for negative numbers.
Right Shifts For signed numbers, the sign bit is used to fill the vacated bit positions. In other words, if the number is positive, 0 is used, and if the number is negative, 1 is used. The result of a right-shift of a signed negative number is implementation-dependent.
Python only has logical shift operators but no arithmetic shift operators. So how to achieve arithmetic right shift in python for signed and unsigned values?
Python actually only has arithmetic shift operators:
n
is exactly the same as multiplying by 2 to the power n
for both positive an negative values.n
depends on whether the value being shifted is negative or not. Positive values are divided by 2 to the power of n
and rounded toward 0, whereas negative values behave as if there was an infinite string of 1
bits extending on the most significant bit side, a side effect of two's complement representation of negative numbers. This translates into a single mathematical equivalence: right shifting an integer by a positive integral quantity n
is evaluated as dividing it by 2 to the power of n
and rounding the result toward negative infinity, Math.floor(value / 2**n)
If you want to simulate unsigned right shift of negative values, as available in java and javascript, you must convert the negative value to a positive value with the fixed number of bits you are considering. Right shifting that will give the expected value:
x = -1
x32 = x & 0xffffffff # convert to 32-bit unsigned value
x >> 8 # produces -1
x32 >> 8 # produces 0x00ffffff
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