Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Arithmetic shift operation in C

Reply
Thread Tools

Arithmetic shift operation in C

 
 
Mehta Shailendrakumar
Guest
Posts: n/a
 
      06-15-2005
Hello,
Can anyone suggest me operator to perform arithmetic shift in C?
May it be for a perticular compiler.

Thank you in advance.

Regards,
Shailendra


 
Reply With Quote
 
 
 
 
Jean-Claude Arbaut
Guest
Posts: n/a
 
      06-15-2005



Le 15/06/2005 15:22, dans d8pa1r$s8d$(E-Mail Removed), «*Mehta
Shailendrakumar*» <(E-Mail Removed)> a écrit*:

> Hello,
> Can anyone suggest me operator to perform arithmetic shift in C?
> May it be for a perticular compiler.
>
> Thank you in advance.
>
> Regards,
> Shailendra
>
>



Ops are << and >>.
* n << k = n * 2^k
* n >> k = n / 2^k
But be careful with signed/unsigned differences,
As in the signed case, a high oerder bit is propagated
(to keep sign correct).
Just write a "toy program" to make some tests.

 
Reply With Quote
 
 
 
 
Tim Prince
Guest
Posts: n/a
 
      06-15-2005
Mehta Shailendrakumar wrote:
> Hello,
> Can anyone suggest me operator to perform arithmetic shift in C?
> May it be for a perticular compiler.
>

On many machines, >> operating on a signed int type would be called an
arithmetic right shift.
 
Reply With Quote
 
Mehta Shailendrakumar
Guest
Posts: n/a
 
      06-15-2005
Hi,

Thanks for the reply.
But in case of right shift of signed integer, I have observed that sign
doesn't get propogated and shift is performed as logical shift.
Does C standard states that ">>" operator is only for logical shift and not
for arithmetic shift?

Thank you.

Regards,
Shailendra

"Jean-Claude Arbaut" <(E-Mail Removed)> wrote in message
news:BED5FA80.4436%(E-Mail Removed)...
>
>
>
> Le 15/06/2005 15:22, dans d8pa1r$s8d$(E-Mail Removed), « Mehta
> Shailendrakumar » <(E-Mail Removed)> a écrit :
>
> > Hello,
> > Can anyone suggest me operator to perform arithmetic shift in C?
> > May it be for a perticular compiler.
> >
> > Thank you in advance.
> >
> > Regards,
> > Shailendra



 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      06-15-2005
Mehta Shailendrakumar wrote:
>
> Hello,
> Can anyone suggest me operator to perform arithmetic shift in C?
> May it be for a perticular compiler.


An arithmetic shift is what you get
when you mutliply or divide an integer by 2.

Shifting negative values is implementation defined.
Two variations that I'm aware of are
"logical shift" and "arithmetic shift".
For unsigned types, logical and arithmetic shifts are the same.

--
pete
 
Reply With Quote
 
Jean-Claude Arbaut
Guest
Posts: n/a
 
      06-15-2005



Le 15/06/2005 15:43, dans d8pb9h$4r$(E-Mail Removed), «*Mehta
Shailendrakumar*» <(E-Mail Removed)> a écrit*:

> Hi,
>
> Thanks for the reply.
> But in case of right shift of signed integer, I have observed that sign
> doesn't get propogated and shift is performed as logical shift.


It should ! Are you sure you declare your variables as "signed" ?
Could you post your piece of code ?

> Does C standard states that ">>" operator is only for logical shift and not
> for arithmetic shift?
>
> Thank you.
>
> Regards,
> Shailendra
>
>


 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      06-15-2005
Mehta Shailendrakumar wrote:
>
> Hi,
>
> Thanks for the reply.
> But in case of right shift of signed integer,
> I have observed that sign
> doesn't get propogated and shift is performed as logical shift.
> Does C standard states that ">>"
> operator is only for logical shift and not
> for arithmetic shift?


No, either type of shift may be done by the >> operator.

If you want an arithmetic shift, then use an arithmetic operator.

integer /= 2

--
pete
 
Reply With Quote
 
Mehta Shailendrakumar
Guest
Posts: n/a
 
      06-15-2005
Hi,

I checked again on signed integer.
It works properly.
Thank you.

Regards,
Shailendra
"Jean-Claude Arbaut" <(E-Mail Removed)> wrote in message
news:BED5FE7F.447A%(E-Mail Removed)...
>
>
>
> Le 15/06/2005 15:43, dans d8pb9h$4r$(E-Mail Removed), « Mehta
> Shailendrakumar » <(E-Mail Removed)> a écrit :
>
> > Hi,
> >
> > Thanks for the reply.
> > But in case of right shift of signed integer, I have observed that sign
> > doesn't get propogated and shift is performed as logical shift.

>
> It should ! Are you sure you declare your variables as "signed" ?
> Could you post your piece of code ?
>
> > Does C standard states that ">>" operator is only for logical shift and

not
> > for arithmetic shift?
> >
> > Thank you.
> >
> > Regards,
> > Shailendra
> >
> >

>



 
Reply With Quote
 
Chris Dollin
Guest
Posts: n/a
 
      06-15-2005
Jean-Claude Arbaut wrote:

> Le 15/06/2005 15:43, dans d8pb9h$4r$(E-Mail Removed), « Mehta
> Shailendrakumar » <(E-Mail Removed)> a écrit :
>
>> Hi,
>>
>> Thanks for the reply.
>> But in case of right shift of signed integer, I have observed that sign
>> doesn't get propogated and shift is performed as logical shift.

>
> It should !


Really? Are you sure that C requires this?

--
Chris "because, you know, it doesn't" Dollin
It's called *extreme* programming, not *stupid* programming.
 
Reply With Quote
 
Villy Kruse
Guest
Posts: n/a
 
      06-15-2005
On Wed, 15 Jun 2005 13:58:59 GMT,
pete <(E-Mail Removed)> wrote:


>
> If you want an arithmetic shift, then use an arithmetic operator.
>
> integer /= 2
>


Carefull with negative integers.

#include <stdlib.h>
#include <stdio.h>

int main ()
{
printf("%x\n", -1235);
printf("%x\n", -1235 / 2);
printf("%x\n", -1235 >> 1);
return 0;
}


Villy
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java left shift and right shift operators. Sanny Java 38 04-29-2011 10:02 PM
Boolean operation and arithmetic operation Buzz Lightyear C++ 10 08-12-2009 01:27 PM
Left Shift / Right Shift Operators Santosh Nayak C Programming 16 11-30-2006 05:10 PM
Shift - byte[] buf shift Roberto Gallo Java 3 01-27-2004 04:26 PM
left shift then right shift an unsigned int Wenjie C++ 3 07-11-2003 08:22 PM



Advertisments