Velocity Reviews > Arithmetic shift operation in C

Arithmetic shift operation in C

Richard Bos
Guest
Posts: n/a

 06-15-2005
Jean-Claude Arbaut <(E-Mail Removed)> wrote:

> Le 15/06/2005 15:43, dans d8pb9h\$4r\$(E-Mail Removed), «*Mehta
> Shailendrakumar*» <(E-Mail Removed)> 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

Jean-Claude Arbaut
Guest
Posts: n/a

 06-15-2005

Le 15/06/2005 16:27, dans d8pds6\$8rp\$(E-Mail Removed), «*Chris
Dollin*» <(E-Mail Removed)> a écrit*:

> Jean-Claude Arbaut wrote:
>
>> Le 15/06/2005 15:43, dans d8pb9h\$4r\$(E-Mail Removed), « Mehta
>> Shailendrakumar » <(E-Mail Removed)> a écrit :
>>
>>> Hi,
>>>
>>> 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
Guest
Posts: n/a

 06-15-2005

Le 15/06/2005 16:27, dans d8pds6\$8rp\$(E-Mail Removed), «*Chris
Dollin*» <(E-Mail Removed)> a écrit*:

> Jean-Claude Arbaut wrote:
>
>> Le 15/06/2005 15:43, dans d8pb9h\$4r\$(E-Mail Removed), « Mehta
>> Shailendrakumar » <(E-Mail Removed)> a écrit :
>>
>>> Hi,
>>>
>>> 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.
"""

Eric Sosman
Guest
Posts: n/a

 06-15-2005

Jean-Claude Arbaut wrote:
>
>
> Le 15/06/2005 16:27, dans d8pds6\$8rp\$(E-Mail Removed), « Chris
> Dollin » <(E-Mail Removed)> a écrit :
>
>
>>Jean-Claude Arbaut wrote:
>>
>>
>>>Le 15/06/2005 15:43, dans d8pb9h\$4r\$(E-Mail Removed), « Mehta
>>>Shailendrakumar » <(E-Mail Removed)> a écrit :
>>>
>>>
>>>>Hi,
>>>>
>>>>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.
> """

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.")

--
http://www.velocityreviews.com/forums/(E-Mail Removed)

Jean-Claude Arbaut
Guest
Posts: n/a

 06-15-2005

>> 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

> 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.

pete
Guest
Posts: n/a

 06-15-2005
Villy Kruse wrote:
>
> 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;
> }

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

CBFalconer
Guest
Posts: n/a

 06-15-2005
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

Eric Sosman
Guest
Posts: n/a

 06-15-2005

pete wrote:
> Villy Kruse wrote:
>
>>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;
>>}

>
>
> 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
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>

--
(E-Mail Removed)

pete
Guest
Posts: n/a

 06-15-2005
Eric Sosman wrote:
>
> pete wrote:
> > Villy Kruse wrote:
> >
> >>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;
> >>}

> >
> >
> > 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

Dik T. Winter
Guest
Posts: n/a

 06-16-2005
In article <(E-Mail Removed) .nl> nobody writes:
> 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.

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/

 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 OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Sanny Java 38 04-29-2011 10:02 PM Buzz Lightyear C++ 10 08-12-2009 01:27 PM Santosh Nayak C Programming 16 11-30-2006 05:10 PM Roberto Gallo Java 3 01-27-2004 04:26 PM Wenjie C++ 3 07-11-2003 08:22 PM