Why does the "bitshift" function not behave as expected with signed integers in MATLAB R2020b?

When working with the "bitshift" function, I found that it does not saturate at the maximum positive value for "int32" and "int64" data types. It also does not shift 1's from the left when right-shifting a negative number to maintain its sign. It appears that "bitshift" behaves identically for "uint" (unsigned integer) and "int" (signed integer) data types, which is not expected. Here are the examples below.
Positive number:
>> dec2hex(bitshift(int32(hex2dec('3FFFFFFF')), 2), 8)
ans =
'FFFFFFFC'
Expected Result: '7FFFFFFF' for int32 as it should saturate, but the actual result is 'FFFFFFFC'.
Negative number:
>> dec2hex(bitshift(int32(hex2dec('FFFFFFFF')), -1), 8)
ans =
'3FFFFFFF'
Expected Result: 'FFFFFFFF' to maintain the negative sign, but the actual result is '3FFFFFFF'.
Is this behavior a bug, or is there a misunderstanding regarding the "bitshift" function with signed integers?

 Accepted Answer

The behavior you're observing is not a bug, but rather a characteristic of how the "bitshift" function operates in MATLAB. The "bitshift" function in MATLAB performs a binary shift on the bits of an integer, moving all bits to the left or right by a specified number of places. It does not perform arithmetic shifting for signed integers or saturation at the maximum or minimum values of the data type.
For unsigned integers, a left shift introduces zeros on the right, and bits shifted off the left end are discarded. For right shifts, zeros are introduced on the left, and bits shifted off the right end are discarded.
For signed integers, the behavior is similar; the "bitshift" function does not consider the sign bit separately. It treats the signed integers as a collection of bits. Therefore, when shifting to the left, bits are still discarded off the end, and zeros are still added on the right. When shifting to the right, zeros are added on the left regardless of the sign of the number, which is why you see '3FFFFFFF' instead of 'FFFFFFFF'.
Let's consider your examples:
Positive number: The result of shifting '3FFFFFFF' left by 2 bits is 'FFFFFFFC'. This is because the two leftmost '3' bits (0011 in binary) are shifted out, and '00' (in binary) is added on the right.
Negative number: The result of shifting 'FFFFFFFF' (which is -1 in signed int32) right by 1 bit is '7FFFFFFF'. This occurs because "bitshift" adds a zero on the left, not preserving the sign bit as an arithmetic shift would.
If you need arithmetic shifting for signed integers, where the sign bit is replicated to preserve the sign of the number, you would need to implement a custom function or use a different approach.
For saturation, MATLAB does not include this behavior in the "bitshift" function. If you want to perform saturation, you would need to check the results of the shift operation and manually cap them at the maximum or minimum values for the data type.
 

More Answers (0)

Products

Release

R2020b

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!