Why does the "bitshift" function not behave as expected with signed integers in MATLAB R2020b?
Show older comments
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
More Answers (0)
Categories
Find more on MATLAB Data API for C++ in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!