right shift of integer variable
显示 更早的评论
Hi,
I want to do a right shift to a give signed integer variable. Can any help me with the syntax please.
My C-code looks like below.
"a = b>>12;"
where the variables 'a' and 'b' are signed integer.
I tried several methods like 'bitsra', 'bitshift' etc. But I was not succesful. Kindly help me please.
采纳的回答
更多回答(4 个)
Richard Brown
2012-4-24
0 个投票
bitshift is for unsigned integers. What exactly do you want your signed right shift to do?
NDKA
2012-4-24
0 个投票
2 个评论
Walter Roberson
2012-4-24
In int16, (-8192) >> 12 is *not* 2: it is either -2 (sign fill) or 6 (no sign fill).
Jan
2012-4-24
"b / 4096" is useful. Do you have a good reason to avoid this?
Richard Brown
2012-4-24
0 个投票
the C language doesn't officially specify what happens on a right shift of a negative signed integer, it's up to any given compiler to decide. I guess you need to decide what sign you want, and then right shift an unsigned version of it. Or you could create a mex file that calls the C >> operator on your variables
Geoff
2012-4-24
0 个投票
MatLab is pretty infuriating sometimes.
For example, if you cast a float as an int, it 'helpfully' rounds the value instead of truncating.
And if you cast a negative integer as unsigned, it returns zero.
Don't get me started on MatLab's fscanf function!
If you want to do a lot of shifting and don't want to multiply or divide by 2^shift, AND don't want to implement a bitshift wrapper to 'cure' the negative case, then I guess you'll need to write it in C and use MEX.
Remember, it's doubtful whether you will get any useful speed advantage using MatLab's bitshift, if that's what you want. Call me cynical =) I haven't actually tested that theory.
类别
在 帮助中心 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!