Bitshift cannot work on negative numbers
11 ビュー (過去 30 日間)
古いコメントを表示
Yuxin Gao
2021 年 6 月 9 日
コメント済み: Sulaymon Eshkabilov
2021 年 6 月 12 日
I checked information of function"bitshift" on https://ww2.mathworks.cn/help/matlab/ref/bitshift.html?lang=en and find it can work for negative values:
- If k is negative and A is negative, then MATLAB shifts the bits to the right and inserts |k| 1-bits on the left.
but when I applied it, if failed and report as below:
bitshift(-6,1)
Error using bitshift
Double inputs must have integer values in
the range of ASSUMEDTYPE.
My version is 2019b, is it beacuse of version or something else?
By the way, is there any website that allow us to refer to functions in previous version?
0 件のコメント
採用された回答
Sulaymon Eshkabilov
2021 年 6 月 9 日
You've overlooked to specify the integer data format type, e.g.:
bitshift(-6,1, 'int8')
bitshift(-6,-1, 'int8')
Works perfectly ok.
4 件のコメント
その他の回答 (1 件)
William
2021 年 6 月 9 日
I believe this is happening for the following reason: The documentation states that "If A is a double array, and ASSUMEDTYPE is not specified, then MATLAB treats A as an unsigned 64-bit integer." What this means is that the function implicitly sets the ASSUMEDTYPE argument to 'uint64'. Since your value of -6 is signed, and is therefore not in the range of a 'uint64', the function issues the error message. This is according to the second rule: "If ASSUMEDTYPE is specified, then all elements in A must have integer values within the range of ASSUMEDTYPE."
You can fix this by specifying that the assumed type is a signed integer rather than an unsigned integer. For example, you can use bitshift(-6,1,'int64'). But I would agree with you that the documentation is unclear.
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!