필터 지우기
필터 지우기

right shift of integer variable

조회 수: 4 (최근 30일)
NDKA
NDKA 2012년 4월 24일
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.

채택된 답변

NDKA
NDKA 2012년 4월 24일
Thanks for your reply Richard. My data is random, sometimes it is positive and sometimes it can be negative i.e. int16.
So, you mean, there is no direct command in matlab to do right/left shift of an signed integer variable (int16)?
Please advise. Thank You
  댓글 수: 2
Richard Brown
Richard Brown 2012년 4월 24일
No there isn't.
p.s. when replying to answers, use comments (like this one) rather than writing new answers
NDKA
NDKA 2012년 4월 24일
Its sad, but I'm glad I now I have to find another way to solve my issue. Thank You Richard.

댓글을 달려면 로그인하십시오.

추가 답변 (4개)

Richard Brown
Richard Brown 2012년 4월 24일
bitshift is for unsigned integers. What exactly do you want your signed right shift to do?

NDKA
NDKA 2012년 4월 24일
Just like in C-language. For example, if the value of 'b' is '-8192', then if I execute the below line, I expect the value of 'a' as '2'.
" a = b>>12 "
I want the equalant matlab code for the above. I know I can just use divide operator "a = b/4096", but I'd like to use a shift operator here.
Thanks a lot Richard.
  댓글 수: 2
Walter Roberson
Walter Roberson 2012년 4월 24일
In int16, (-8192) >> 12 is *not* 2: it is either -2 (sign fill) or 6 (no sign fill).
Jan
Jan 2012년 4월 24일
"b / 4096" is useful. Do you have a good reason to avoid this?

댓글을 달려면 로그인하십시오.


Richard Brown
Richard Brown 2012년 4월 24일
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
Geoff 2012년 4월 24일
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.
  댓글 수: 1
NDKA
NDKA 2012년 4월 24일
I hope Matlab will consider to include this option in future. As of now, I'll try to find an alternate option. Thanks for your comments Geoff, it really helped me.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by