lak
Guest
Posts: n/a

 09-20-2007
i know left and right shift normally,but i cant know what happens if
it is negative.
for example
int x=-2;
x<<=1;//what happens here

Pietro Cerutti
Guest
Posts: n/a

 09-20-2007
lak wrote:
> i know left and right shift normally,but i cant know what happens if
> it is negative.
> for example
> int x=-2;
> x<<=1;//what happens here
>

nothing different from usual.. the bit representation of -2 is shifted
one bit to the left...

> cat test_shift.c

#include <stdio.h>
int main(void)
{

int k = -1;
printf("k is %d (%x)\n", k, k);
k<<=4;
printf("k is %d (%x)\n", k, k);

return (0);
}

> gcc -Wall -o test_shift test_shift.c && ./test_shift

k is -1 (ffffffff)
k is -16 (fffffff0)

Pietro Cerutti

Richard Bos
Guest
Posts: n/a

 09-20-2007
lak <(E-Mail Removed)> wrote:

> i know left and right shift normally,but i cant know what happens if
> it is negative.
> for example
> int x=-2;
> x<<=1;//what happens here

That is correct: you cannot know that.

From paragraph 6.5.7 in the ISO C Standard:

# 4 The result of E1 << E2 is E1 left-shifted E2 bit positions; vacated
# bits are filled with zeros. If E1 has an unsigned type, the value of
# the result is E1 x 2 E2 ,reduced modulo one more than the maximum
# value representable in the result type. If E1 has a signed type and
# nonnegative value, and E1 x 2 E2 is representable in the result
# type, then that is the resulting value; otherwise, the behavior is
# undefined. ^^^^^^^^^^^^^^^^^^^^^^^^^^
^^^^^^^^^

Note the under^^^lined bit. Since in your case x is neither an unsigned
integer, nor a signed integer with a positive value, the behaviour of
your code is undefined; and this means that, as far as ISO C is
concerned, you cannot know what happens. (It may be possible to discover
what happens on a particular computer using a particular compiler with
particular compilation settings, but I advise against it; on the next
system, or even on the next level of optimisation, the result can easily
be different.)

Richard

Charlie Gordon
Guest
Posts: n/a

 09-20-2007
"Richard Bos" <(E-Mail Removed)> a écrit dans le message de news:
http://www.velocityreviews.com/forums/(E-Mail Removed)4all.nl...
> lak <(E-Mail Removed)> wrote:
>
>> i know left and right shift normally,but i cant know what happens if
>> it is negative.
>> for example
>> int x=-2;
>> x<<=1;//what happens here

>
> That is correct: you cannot know that.
>
> From paragraph 6.5.7 in the ISO C Standard:
>
> # 4 The result of E1 << E2 is E1 left-shifted E2 bit positions; vacated
> # bits are filled with zeros. If E1 has an unsigned type, the value of
> # the result is E1 x 2 E2 ,reduced modulo one more than the maximum
> # value representable in the result type. If E1 has a signed type and
> # nonnegative value, and E1 x 2 E2 is representable in the result
> # type, then that is the resulting value; otherwise, the behavior is
> # undefined. ^^^^^^^^^^^^^^^^^^^^^^^^^^
> ^^^^^^^^^
>
> Note the under^^^lined bit. Since in your case x is neither an unsigned
> integer, nor a signed integer with a positive value, the behaviour of
> your code is undefined; and this means that, as far as ISO C is
> concerned, you cannot know what happens. (It may be possible to discover
> what happens on a particular computer using a particular compiler with
> particular compilation settings, but I advise against it; on the next
> system, or even on the next level of optimisation, the result can easily
> be different.)

Richard is correct.
However, if you expected x <<= 1 to be equivalent to x += x, as would
"normally" be the case on regular 2s-complement machines, you might as well
write the latter.

--
Chqrlie.

Pietro Cerutti
Guest
Posts: n/a

 09-20-2007
Pietro Cerutti wrote:
> lak wrote:
>> i know left and right shift normally,but i cant know what happens if
>> it is negative.
>> for example
>> int x=-2;
>> x<<=1;//what happens here
>>

>
> nothing different from usual.. the bit representation of -2 is shifted
> one bit to the left...

Umh, I have to apologize.. my sentence is actually incorrect. That's
true for right-shifting, while for left-shifting a negative left-hand
operand invokes UB

--
Pietro Cerutti

Kenneth Brody
Guest
Posts: n/a

 09-20-2007
Charlie Gordon wrote:
[... bit-shifting negative numbers ...]
> However, if you expected x <<= 1 to be equivalent to x += x, as would
> "normally" be the case on regular 2s-complement machines, you might
> as well write the latter.

Okay, nit-pick time related to UB.

Why doesn't the statement:

x += x;

violate 6.5p2:

Between the previous and next sequence point an object shall
have its stored value modified at most once by the evaluation
of an expression. Furthermore, the prior value shall be read
only to determine the value to be stored.

Yes, I see that footnote 71 says that "i = i + 1" is allowed by the
paragraph, but why is the "x" on the right of "+=" not violating the
"shall be read only to determine the value to be stored"? How is
this different from "y = x + x++;" in the use of "x"?

Obviously, something like "x += x;" must be allowed, but what is it

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <(E-Mail Removed)>

CBFalconer
Guest
Posts: n/a

 09-20-2007
Pietro Cerutti wrote:
> Pietro Cerutti wrote:
>> lak wrote:
>>
>>> i know left and right shift normally,but i cant know what
>>> happens if it is negative. for example
>>> int x=-2;
>>> x<<=1;//what happens here

>>
>> nothing different from usual.. the bit representation of -2 is
>> shifted one bit to the left...

>
> Umh, I have to apologize.. my sentence is actually incorrect.
> That's true for right-shifting, while for left-shifting a
> negative left-hand operand invokes UB

Since lak appears to be a newbie, explain that UB means "undefined
behaviour". In other words, don't do that. Also for positive
operands that overflow.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Flash Gordon
Guest
Posts: n/a

 09-20-2007
Pietro Cerutti wrote, On 20/09/07 14:45:
> Pietro Cerutti wrote:
>> lak wrote:
>>> i know left and right shift normally,but i cant know what happens if
>>> it is negative.
>>> for example
>>> int x=-2;
>>> x<<=1;//what happens here
>>>

>> nothing different from usual.. the bit representation of -2 is shifted
>> one bit to the left...

>
> Umh, I have to apologize.. my sentence is actually incorrect. That's
> true for right-shifting, while for left-shifting a negative left-hand
> operand invokes UB

For right shifting it is implementation defined, so you were just wrong.
--
Flash Gordon

Flash Gordon
Guest
Posts: n/a

 09-20-2007
Charlie Gordon wrote, On 20/09/07 13:51:
> "Richard Bos" <(E-Mail Removed)> a écrit dans le message de news:
> (E-Mail Removed)4all.nl...
>> lak <(E-Mail Removed)> wrote:
>>
>>> i know left and right shift normally,but i cant know what happens if
>>> it is negative.
>>> for example
>>> int x=-2;
>>> x<<=1;//what happens here

>> That is correct: you cannot know that.

<snip>

> Richard is correct.
> However, if you expected x <<= 1 to be equivalent to x += x, as would
> "normally" be the case on regular 2s-complement machines, you might as well
> write the latter.

Personally I would write it as x *= 2.
--
Flash Gordon

user923005
Guest
Posts: n/a

 09-20-2007
On Sep 20, 10:33 am, Kenneth Brody <(E-Mail Removed)> wrote:
> Charlie Gordon wrote:
>
> [... bit-shifting negative numbers ...]
>
> > However, if you expected x <<= 1 to be equivalent to x += x, as would
> > "normally" be the case on regular 2s-complement machines, you might
> > as well write the latter.

>
> Okay, nit-pick time related to UB.
>
> Why doesn't the statement:
>
> x += x;
>
> violate 6.5p2:
>
> Between the previous and next sequence point an object shall
> have its stored value modified at most once by the evaluation
> of an expression. Furthermore, the prior value shall be read
> only to determine the value to be stored.
>
> Yes, I see that footnote 71 says that "i = i + 1" is allowed by the
> paragraph, but why is the "x" on the right of "+=" not violating the
> "shall be read only to determine the value to be stored"? How is
> this different from "y = x + x++;" in the use of "x"?
>
> Obviously, something like "x += x;" must be allowed, but what is it
> about 6.5p2 that allows it?

"Furthermore, the prior value shall be read only to determine the
value to be stored."

Quite frankly, in this case, I simply don't see how it would be
possible for the compiler to get it wrong.

For an instance like:

i = ++i;

there are two modifications of i, so it's right out.

But how is:
x += x;
more dangerous than (for instance):
x = x;
Both of which have to examine the contents of x use those contents to
modify x (though this second instance can be thrown out by the
compiler if it chooses because of the 'as if' rule.)

For the instance of:
y = x + x++;
We don't even need the y. This is also undefined behavior:

#include <stdlib.h>
int t(void)
{
int x = rand();
return x + x++;
}

We are adding x + <something>
We are also incrementing x.
There is no sequence point.

 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 a_Conan VHDL 0 08-23-2005 11:51 AM jahaya@gmail.com VHDL 1 07-22-2005 10:09 AM vidhya Java 0 07-20-2005 06:21 AM Stefan Duenser VHDL 4 12-08-2004 11:00 AM walala VHDL 3 11-21-2003 09:37 AM