473,659 Members | 3,162 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

about shifting

lak
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

Sep 20 '07 #1
16 1550
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
Sep 20 '07 #2
lak <la********@gma il.comwrote:
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
Sep 20 '07 #3
"Richard Bos" <rl*@hoekstra-uitgeverij.nla écrit dans le message de news:
46************* ****@news.xs4al l.nl...
lak <la********@gma il.comwrote:
>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.
Sep 20 '07 #4
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
Sep 20 '07 #5
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
Sep 20 '07 #6
Charlie Gordon wrote, On 20/09/07 13:51:
"Richard Bos" <rl*@hoekstra-uitgeverij.nla écrit dans le message de news:
46************* ****@news.xs4al l.nl...
>lak <la********@gma il.comwrote:
>>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
Sep 20 '07 #7
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?

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer .h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th***** ********@gmail. com>
Sep 20 '07 #8
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

Sep 20 '07 #9
On Sep 20, 10:33 am, Kenneth Brody <kenbr...@spamc op.netwrote:
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?
"Furthermor e, 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.

Sep 20 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
4398
by: David Stockwell | last post by:
Hi, My background is c/c++ and java. I'm learning python at this point. My question is does python share java's peculiar mode of bit shifting, or does python adhere closer to c's bit shifting? in java there are 3 kinds of bit shifts: << (shift left) >> (preserve the sign bit as we move right ) >>> (0 filled on the left as we move right)
77
5667
by: nospam | last post by:
Reasons for a 3-tier achitecture for the WEB? (NOTE: I said, WEB, NOT WINDOWS. DON'T shoot your mouth off if you don't understand the difference.) I hear only one reason and that's to switch a database from SQL Server to Oracle or DB2 or vice versa... and that's it.... And a lot of these enterprises don't need it as they already know what database they are going to use and they don't plan on switching in and out database in the first...
3
4662
by: Joe C | last post by:
I have some code that performs bitwise operations on files. I'm trying to make the code portable on different endian systems. This is not work/school related...just trying to learn/understand. My computer is little endian 32-bit (intel, imagine that). My code deals with binary data in files, and I've been treating all data as native words (32-bit) for performance reasons. I found out that if I treat the data as long long (64-bit) I...
9
8733
by: GGG | last post by:
Noticed something odd in the way bit shifting was working today. As far as I have ever heard, shifting will shift in zeros(signed ints aside) However I foudn something odd when I am shifting exactly the number of bits in a value... i.e. uint32_t x = 5;
2
2188
by: jagivens | last post by:
Hi, I have two identical programs that encrypt characters. One is written in C++, and it works, but the other one is written in C, and it does not work. I have copied the code below. There is a problem somewhere in the while loop in the C program. It goes through the code one full time (and updates the counter) before asking for user input - thus it updates the counter twice for every one input. Since I'm not so familiar with C,...
2
2055
by: salsipius | last post by:
Can someone please help me clarify the below code. I think the shifting has to do with converting datatypes and/or loss of data but am not really clear on the details, could you help shed some light please? // Allocate array for( i = 0; i < Length; i++ ) { //pArray_00 is a BYTE Array -- Here a cast is used because
10
2400
by: chanma | last post by:
code1:var x=0xf0000000; alert(x); output:4026531840 code2:var b=0xf<<28; alert(b); output:-268435456
10
10688
by: krunalb | last post by:
Hi, I am trying to shift unsigned long long value by 64 bits and this is what i get #include <stdio.h> int main() { unsigned short shiftby= 64;
12
2252
by: Boltar | last post by:
I seem to be having yet more wierd issue with bit shifting. It seems the following code doesnt do anything under gcc (ie it returns -1 as both results). Anyone know why? Is it another language definition or CPU issue? main() { printf("%d\n",(int)0xFFFFFFFF >1); printf("%d\n",(int)-1 >1); }
0
8428
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8337
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8748
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7359
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2754
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1978
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1739
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.