Arithmetic shift operation in C

Discussion in 'C Programming' started by Mehta Shailendrakumar, Jun 15, 2005.

  1. 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
     
    Mehta Shailendrakumar, Jun 15, 2005
    #1
    1. Advertising

  2. Re : Arithmetic shift operation in C

    Le 15/06/2005 15:22, dans d8pa1r$s8d$, « Mehta
    Shailendrakumar » <> 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.
     
    Jean-Claude Arbaut, Jun 15, 2005
    #2
    1. Advertising

  3. Mehta Shailendrakumar

    Tim Prince Guest

    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.
     
    Tim Prince, Jun 15, 2005
    #3
  4. 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" <> wrote in message
    news:BED5FA80.4436%...
    >
    >
    >
    > Le 15/06/2005 15:22, dans d8pa1r$s8d$, « Mehta
    > Shailendrakumar » <> 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
     
    Mehta Shailendrakumar, Jun 15, 2005
    #4
  5. Mehta Shailendrakumar

    pete Guest

    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
     
    pete, Jun 15, 2005
    #5
  6. Re : Arithmetic shift operation in C

    Le 15/06/2005 15:43, dans d8pb9h$4r$, « Mehta
    Shailendrakumar » <> 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
    >
    >
     
    Jean-Claude Arbaut, Jun 15, 2005
    #6
  7. Mehta Shailendrakumar

    pete Guest

    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
     
    pete, Jun 15, 2005
    #7
  8. Hi,

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

    Regards,
    Shailendra
    "Jean-Claude Arbaut" <> wrote in message
    news:BED5FE7F.447A%...
    >
    >
    >
    > Le 15/06/2005 15:43, dans d8pb9h$4r$, « Mehta
    > Shailendrakumar » <> 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
    > >
    > >

    >
     
    Mehta Shailendrakumar, Jun 15, 2005
    #8
  9. Mehta Shailendrakumar

    Chris Dollin Guest

    Re: Re : Arithmetic shift operation in C

    Jean-Claude Arbaut wrote:

    > Le 15/06/2005 15:43, dans d8pb9h$4r$, « Mehta
    > Shailendrakumar » <> 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.
     
    Chris Dollin, Jun 15, 2005
    #9
  10. Mehta Shailendrakumar

    Villy Kruse Guest

    On Wed, 15 Jun 2005 13:58:59 GMT,
    pete <> 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
     
    Villy Kruse, Jun 15, 2005
    #10
  11. Mehta Shailendrakumar

    Richard Bos Guest

    Re: Re : Arithmetic shift operation in C

    Jean-Claude Arbaut <> wrote:

    > Le 15/06/2005 15:43, dans d8pb9h$4r$, « Mehta
    > Shailendrakumar » <> a écrit :
    >
    > > 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" ?


    No, it needn't. Right shifting a signed, negative integer gives
    implementation-defined results.

    Richard
     
    Richard Bos, Jun 15, 2005
    #11
  12. Re : Re : Arithmetic shift operation in C

    Le 15/06/2005 16:27, dans d8pds6$8rp$, « Chris
    Dollin » <> a écrit :

    > Jean-Claude Arbaut wrote:
    >
    >> Le 15/06/2005 15:43, dans d8pb9h$4r$, « Mehta
    >> Shailendrakumar » <> 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?


    Ok, mea culpa:

    """
    The integer promotions are performed on each of the operands. The type of
    the result is
    that of the promoted left operand. If the value of the right operand is
    negative or is
    greater than or equal to the width of the promoted left operand, the
    behavior is undefined.
    """

    But unless you use a brain-damaged compiler, this is the usual behaviour,
    though not "Standard". Now I wait for quibblings and their crazy counter
    examples.
     
    Jean-Claude Arbaut, Jun 15, 2005
    #12
  13. Re : Re : Arithmetic shift operation in C

    Le 15/06/2005 16:27, dans d8pds6$8rp$, « Chris
    Dollin » <> a écrit :

    > Jean-Claude Arbaut wrote:
    >
    >> Le 15/06/2005 15:43, dans d8pb9h$4r$, « Mehta
    >> Shailendrakumar » <> 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?


    Obviously, I wanted to quote that :)


    """
    The result ofE1 >> E2is E1 right-shifted E2 bit 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/ 2E2. If E1 has a signed type and a negative
    value, the
    resulting value is implementation-defined.
    """
     
    Jean-Claude Arbaut, Jun 15, 2005
    #13
  14. Mehta Shailendrakumar

    Eric Sosman Guest

    Re: Re : Re : Arithmetic shift operation in C

    Jean-Claude Arbaut wrote:
    >
    >
    > Le 15/06/2005 16:27, dans d8pds6$8rp$, « Chris
    > Dollin » <> a écrit :
    >
    >
    >>Jean-Claude Arbaut wrote:
    >>
    >>
    >>>Le 15/06/2005 15:43, dans d8pb9h$4r$, « Mehta
    >>>Shailendrakumar » <> 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?

    >
    >
    > Ok, mea culpa:


    Only your latest -- and, I fear, probably not your last.

    > """
    > The integer promotions are performed on each of the operands. The type of
    > the result is
    > that of the promoted left operand. If the value of the right operand is
    > negative or is
    > greater than or equal to the width of the promoted left operand, the
    > behavior is undefined.
    > """


    What has this to do with the question at hand? It would
    have made equal sense to quote the description of setvbuf().

    > But unless you use a brain-damaged compiler, this is the usual behaviour,
    > though not "Standard". Now I wait for quibblings and their crazy counter
    > examples.


    What is "this" that you say is "usual behavior?" (And why
    is is "quibbling" to point out that you are wrong Yet Again?
    "All people with French-ish names are three meters tall and
    have five eyes on wavy stalks; don't bother me with crazy
    counter-examples.")

    --
     
    Eric Sosman, Jun 15, 2005
    #14
  15. Re : Re : Re : Arithmetic shift operation in C


    >> Ok, mea culpa:

    >
    > Only your latest -- and, I fear, probably not your last.


    Only the first, and hopefully not my last. I don't believe I understand
    everything, did you think so ? I thought NG were to share fruitful
    discussions and informations. Maybe you believe I'm not able to
    do that, but you're not proving you are in this whole post. Sadly.

    > What has this to do with the question at hand? It would
    > have made equal sense to quote the description of setvbuf().


    2nd mea culpa :) It was a copy/paste mistake this time, but I already
    answered.

    > What is "this" that you say is "usual behavior?" (And why


    Really you have no idea ? Never mind.

    > is is "quibbling" to point out that you are wrong Yet Again?


    > "All people with French-ish names are three meters tall and
    > have five eyes on wavy stalks; don't bother me with crazy
    > counter-examples."


    Ah Ah Ah. We have tons of anti-US jokes, but I don't think
    it would be a high mark of intellingence to put them on that
    newsgroup.
     
    Jean-Claude Arbaut, Jun 15, 2005
    #15
  16. Mehta Shailendrakumar

    pete Guest

    Villy Kruse wrote:
    >
    > On Wed, 15 Jun 2005 13:58:59 GMT,
    > pete <> 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;
    > }


    As far as getting an arithmetic shift goes, dividing by two
    *does* get an arithmetic shift for negative integers, always.

    But, you just reminded me that some negative division itself,
    what the arithmetic shift is, is implementation defined in C89.

    ISO/IEC 9899: 1990
    6.3.5 Multiplicative operators

    If either operand is negative,
    whether the result of the / operator is the
    largest integer less than or equal to the algebraic quotient
    or the smallest integer greater than
    or equal to the algebraic quotient is implementation-defined,
    as is the sign of the result of the % operator.
    If the quotient a/b is representable,
    the expression (a/b)*b + a%b shall equal a.

    --
    pete
     
    pete, Jun 15, 2005
    #16
  17. Mehta Shailendrakumar

    CBFalconer Guest

    Mehta Shailendrakumar wrote:
    >
    > Can anyone suggest me operator to perform arithmetic shift in C?
    > May it be for a perticular compiler.


    There is a lot of misinformation in the replies you have received.
    Shift operations in C are only portable, and properly defined, on
    unsigned objects. Anything else can lead to undefined and
    implementation defined behaviour.

    If you want to multiply or divide signed integer quantities by
    powers of two, just do so. The operators are '*' and '/'.

    If there are appropriate shifting sequences available on the end
    machine, the compiler optimizer has the job of finding them. Not
    you.

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
     
    CBFalconer, Jun 15, 2005
    #17
  18. Mehta Shailendrakumar

    Eric Sosman Guest

    pete wrote:
    > Villy Kruse wrote:
    >
    >>On Wed, 15 Jun 2005 13:58:59 GMT,
    >> pete <> 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;
    >>}

    >
    >
    > As far as getting an arithmetic shift goes, dividing by two
    > *does* get an arithmetic shift for negative integers, always.


    Well, not exactly. On two's complement systems, -1 right-
    shifted with sign propagation equals -1, but -1 / 2 is zero
    (under C99 rules).

    > But, you just reminded me that some negative division itself,
    > what the arithmetic shift is, is implementation defined in C89.
    >
    > ISO/IEC 9899: 1990
    > 6.3.5 Multiplicative operators
    >
    > If either operand is negative,
    > whether the result of the / operator is the
    > largest integer less than or equal to the algebraic quotient
    > or the smallest integer greater than
    > or equal to the algebraic quotient is implementation-defined,
    > as is the sign of the result of the % operator.
    > If the quotient a/b is representable,
    > the expression (a/b)*b + a%b shall equal a.


    Yes: C90 permitted the implementation to choose, and
    provided the div() and ldiv() functions when predictable
    behavior was needed. C99 eliminated the choice, but div()
    and ldiv() -- and now lldiv()! -- are still with us.

    <off-topic excuse="intriguing trivia">

    According to the Rationale, the choice was eliminated to
    make C99 comply with Fortran's rules for arithmetic. Ponder
    this next time somebody sneers about "dead languages" -- and
    ponder, too, how some of C's weirdnesses are perpetuated in
    Java. "But we've always done it that way" has more force
    than we may like to admit.

    </off-topic>

    --
     
    Eric Sosman, Jun 15, 2005
    #18
  19. Mehta Shailendrakumar

    pete Guest

    Eric Sosman wrote:
    >
    > pete wrote:
    > > Villy Kruse wrote:
    > >
    > >>On Wed, 15 Jun 2005 13:58:59 GMT,
    > >> pete <> 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;
    > >>}

    > >
    > >
    > > As far as getting an arithmetic shift goes, dividing by two
    > > *does* get an arithmetic shift for negative integers, always.

    >
    > Well, not exactly.
    > On two's complement systems,
    > -1 right-shifted with sign propagation equals -1,
    > but -1 / 2 is zero (under C99 rules).


    I did some googling.
    It turns out that an arithmetic shift,
    isn't exactly what I thought it was.

    --
    pete
     
    pete, Jun 15, 2005
    #19
  20. In article <> nobody writes:
    > On Wed, 15 Jun 2005 13:58:59 GMT,
    > pete <> wrote:

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

    >
    > Carefull with negative integers.


    Indeed.

    > printf("%x\n", -1235 >> 1);


    Undefined behaviour.
    --
    dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
    home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
     
    Dik T. Winter, Jun 16, 2005
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roberto Gallo

    Shift - byte[] buf shift

    Roberto Gallo, Jan 27, 2004, in forum: Java
    Replies:
    3
    Views:
    2,250
    Thomas Schodt
    Jan 27, 2004
  2. Wenjie
    Replies:
    3
    Views:
    1,096
    Ron Samuel Klatchko
    Jul 11, 2003
  3. Santosh Nayak

    Left Shift / Right Shift Operators

    Santosh Nayak, Nov 30, 2006, in forum: C Programming
    Replies:
    16
    Views:
    1,507
    CBFalconer
    Nov 30, 2006
  4. Buzz Lightyear
    Replies:
    10
    Views:
    1,162
    Alexander Bartolich
    Aug 12, 2009
  5. Sanny
    Replies:
    38
    Views:
    3,574
    Thomas Richter
    Apr 29, 2011
Loading...

Share This Page