f



How to do >>(right shift) and <<(left shift) in Matlab?

Hi all,

How >>(right shift) and <<(left shift) 2'complement number
(both positive and negative integer).

Is x>>2 equal to floor(x/4)?

And is x<<2 equal to x*4?

Best regards,
Davy

0
zhushenli (384)
6/14/2006 6:49:07 AM
comp.soft-sys.matlab 211264 articles. 25 followers. lunamoonmoon (257) is leader. Post Follow

5 Replies
1186 Views

Similar Articles

[PageSpeed] 42

Soho wrote:
> Hi all,
> 
> How >>(right shift) and <<(left shift) 2'complement number
> (both positive and negative integer).
> 
> Is x>>2 equal to floor(x/4)?
> 
> And is x<<2 equal to x*4?
> 
> Best regards,
> Davy
> 

Does bitshift: 
http://www.mathworks.com/access/helpdesk/help/techdoc/ref/bitshift.html
do what you want?

-- 
--Loren
http://blogs.mathworks.com/loren/
0
loren5358 (941)
6/14/2006 11:37:15 AM
Hi Loren,

Thanks!

The bitshift C = bitshift(A, k)  returns the value of A shifted by k
bits. Input argument A must be an unsigned integer or an array of
unsigned integers.

But I need the signed version.

I used to use floor(x/(2^n)) to emulate x>>n, is it right?

Best regards,
Davy

Loren Shure wrote:
> Soho wrote:
> > Hi all,
> >
> > How >>(right shift) and <<(left shift) 2'complement number
> > (both positive and negative integer).
> >
> > Is x>>2 equal to floor(x/4)?
> >
> > And is x<<2 equal to x*4?
> >
> > Best regards,
> > Davy
> >
>
> Does bitshift:
> http://www.mathworks.com/access/helpdesk/help/techdoc/ref/bitshift.html
> do what you want?
> 
> -- 
> --Loren
> http://blogs.mathworks.com/loren/

0
zhushenli (384)
6/15/2006 6:02:15 AM
Soho wrote:
>
>
> Hi all,
>
> How >>(right shift) and <<(left shift) 2'complement
number
> (both positive and negative integer).
>
> Is x>>2 equal to floor(x/4)?
>
> And is x<<2 equal to x*4?

Assuming you mean the operator ">>" from the C language, this
is a quote from the ANSI standard:

"The result of E1>>E2 is E1 right-shifted E2 positions. If E1
has an unsigned type or if E1 has a signed type and a nonnegative
value, the value of the result is the integral part of the quotient
of E1 divided by the quantity, 1 raised to the power E2. If E1 has a
signed type and a negative value, the resulting value is
implementation-defined."

Note the last sentence. It means that it's up to the implementation
to decide what to do with x>>2 if x is negative. My C compiler
chooses to preserve the sign bit and right shift the others for
signed ints and it seems ML does too (even though the help for
bitshit suggests it's not allowed on a negative number).

If you want to confuse yourself even further, try this little
program. My compiler does different things to the union if it thinks
it's holding signed or unsigned data.

#include <stdio.h>
int main(int argc, char *argv[])
{
  union
  {
    char sc;
    unsigned char uc;
  }x, y, z;

  x.sc = -1;
  y.sc=x.sc>>1;
  z.uc=x.uc>>1;

  printf("%d %d\n", x.sc, x.uc);
  printf("%d %d\n", y.sc, y.uc);
  printf("%d %d\n", z.sc, z.uc);
}
0
6/15/2006 12:11:04 PM
<SNIP>

> bitshit 


hopefully not :-)


michael
0
6/15/2006 12:25:31 PM
Hi,

I copy the x >> y definition from a video standard (H.264).

Arithmetic right shift of a two's complement integer representation
of x by y binary digits. This function is defined only for positive
integer values of y. Bits shifted into the MSBs as a result of the
right shift have a value equal to the MSB of x prior to the shift
operation.

Did matlab provide a arithmetic shift function?

Best regards,
Davy

Steve Amphlett wrote:
> Soho wrote:
> >
> >
> > Hi all,
> >
> > How >>(right shift) and <<(left shift) 2'complement
> number
> > (both positive and negative integer).
> >
> > Is x>>2 equal to floor(x/4)?
> >
> > And is x<<2 equal to x*4?
>
> Assuming you mean the operator ">>" from the C language, this
> is a quote from the ANSI standard:
>
> "The result of E1>>E2 is E1 right-shifted E2 positions. If E1
> has an unsigned type or if E1 has a signed type and a nonnegative
> value, the value of the result is the integral part of the quotient
> of E1 divided by the quantity, 1 raised to the power E2. If E1 has a
> signed type and a negative value, the resulting value is
> implementation-defined."
>
> Note the last sentence. It means that it's up to the implementation
> to decide what to do with x>>2 if x is negative. My C compiler
> chooses to preserve the sign bit and right shift the others for
> signed ints and it seems ML does too (even though the help for
> bitshit suggests it's not allowed on a negative number).
>
> If you want to confuse yourself even further, try this little
> program. My compiler does different things to the union if it thinks
> it's holding signed or unsigned data.
>
> #include <stdio.h>
> int main(int argc, char *argv[])
> {
>   union
>   {
>     char sc;
>     unsigned char uc;
>   }x, y, z;
>
>   x.sc = -1;
>   y.sc=x.sc>>1;
>   z.uc=x.uc>>1;
>
>   printf("%d %d\n", x.sc, x.uc);
>   printf("%d %d\n", y.sc, y.uc);
>   printf("%d %d\n", z.sc, z.uc);
> }

0
zhushenli (384)
6/16/2006 2:27:52 AM
Reply: