473,399 Members | 2,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

What will happen to "1 << (-1)" in G++?

Left shift by negative numbers, will I get 1/2?

Thanks.

Sep 8 '06 #1
16 1994
Left shift by negative numbers, will I get 1/2?

Why not try it for yourself? The great virtue of computer science is
that in many ways it's an experimental discipline, not a purely
theoretical one.

Sep 8 '06 #2
"Mr. Ken" <Mr. Ken@asdfschrieb im Newsbeitrag
news:45********@news.starhub.net.sg...
Left shift by negative numbers, will I get 1/2?
The result of a shift expression is undefined if its right operand is
negative or greater than or equal to the number of bits used to represent
its left operand.

Heinz

Sep 8 '06 #3

"Heinz Ozwirk" <ho*****@arcor.dewrote in message
news:45**********************@newsspool1.arcor-online.net...
"Mr. Ken" <Mr. Ken@asdfschrieb im Newsbeitrag
news:45********@news.starhub.net.sg...
Left shift by negative numbers, will I get 1/2?

The result of a shift expression is undefined if its right operand is
negative or greater than or equal to the number of bits used to represent
its left operand.

Heinz
Thank you.

Sep 8 '06 #4

"Robert J. Hansen" <ci********@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
Left shift by negative numbers, will I get 1/2?

Why not try it for yourself? The great virtue of computer science is
that in many ways it's an experimental discipline, not a purely
theoretical one.
I received code from other people, but it has the probability of 1 << (-1)..
that's why I asked.

Sep 8 '06 #5
Robert J. Hansen wrote:
Left shift by negative numbers, will I get 1/2?

Why not try it for yourself? The great virtue of computer science is
that in many ways it's an experimental discipline, not a purely
theoretical one.
this is Really Bad Advice. I you want t to know what a language
construct
is supposed to do then check out a reference. The language standard
itself
is often the best. If you distrust your implementation you may want to
run
a test to verify that your implementation is conforming.

Since "1 << (-1)" has undefined behaviour no amount of empirical
testing
will tell you anything. This weeks test may be invalidated next week by
a
change of platform, compiler version or optimisation settings. In
principal
an implementation could be even worse than that. It may depend on some
obscurity in the hardware.
--
Nick Keighley

Egon: Try to imagine all life as you know it stopping instantaneously
and
every molecule in your body exploding at the speed of light.
Ray: Total protonic reversal....
[DS9000K: Behaviour of Negative Shift]

Sep 8 '06 #6
Mr. Ken wrote:
Left shift by negative numbers, will I get 1/2?
Lame troll.

Sep 8 '06 #7
Why not try it for yourself? The great virtue of computer science is
that in many ways it's an experimental discipline, not a purely
theoretical one.

this is Really Bad Advice.
It is really _good_ advice, given that he specifically asked a question
tied to a specific implementation. If he wants to know what happens in
G++ when this is done, the best way is to try it for yourself. It's
not as if the compiler costs a lot of money to download.

Sep 8 '06 #8
Sometimes a bad advice.

Yes. If he were to be asking "according to the Standard, what happens
when...?", then I would have given him an answer citing the Standard.

But if you're going to ask compiler-specific questions, then "try it
yourself and see" is perfectly reasonable advice. Especially when the
real answer is "it's undefined and the compiler can do anything, and so
the only answer to this question of 'what will G++ do with this?' is
'give it to G++ and see'."

Sep 8 '06 #9
Robert J. Hansen wrote:
>Sometimes a bad advice.

Yes. If he were to be asking "according to the Standard, what happens
when...?", then I would have given him an answer citing the Standard.

But if you're going to ask compiler-specific questions, then "try it
yourself and see" is perfectly reasonable advice. Especially when the
real answer is "it's undefined and the compiler can do anything, and so
the only answer to this question of 'what will G++ do with this?' is
'give it to G++ and see'."
Except that G++ could do it differently each and every time you run the
program.

--
Clark S. Cox III
cl*******@gmail.com
Sep 8 '06 #10
Clark S. Cox III wrote:
'give it to G++ and see'."

Except that G++ could do it differently each and every time you run the
program.
Yes, it could. And if you try it a few times, you'll discover if it
does.

I do not understand this hostility towards discovering how your
compiler handles one particular kind of undefined behavior.

Sep 9 '06 #11
Robert J. Hansen wrote:
Clark S. Cox III wrote:
>>'give it to G++ and see'."
Except that G++ could do it differently each and every time you run the
program.

Yes, it could. And if you try it a few times, you'll discover if it
does.
You *still* won't know for sure. It could be the same 20 times in a row,
and then be different on the 21st.

I have personally seen all of the following:
- Undefined behavior that works one way on the development machine, the
production build machine, and several test machines; only to work in a
completely different way on a customer's machine.

- Undefined behavior that works one way with one build of the compiler,
and a different way with the next build.

- Undefined behavior that works one way with one optimization level, and
a different way with another.
I do not understand this hostility towards discovering how your
compiler handles one particular kind of undefined behavior.
Because finding that out provides you with a false sense of security,
and encourages people to write code that makes unwarranted assumptions.

--
Clark S. Cox III
cl*******@gmail.com
Sep 9 '06 #12
You *still* won't know for sure. It could be the same 20 times in a row,
and then be different on the 21st.
Then you know it's not "each and every".
I have personally seen all of the following:
So have I.
Because finding that out provides you with a false sense of security,
and encourages people to write code that makes unwarranted assumptions.
It only encourages a false sense of security if you're dumb enough to
feel secure in undefined behavior. On the other hand, knowing that
"every time I've done this particular bit of undefined behavior in the
past, the results have been like this" is an incredible help in
debugging when you see that exact same pattern of behavior. It's not a
deterministic help, no, but it's an effective heuristic.

If you let the fear of a false sense of security prevent you from
learning how your compiler handles undefined behavior, you have
needlessly and pointlessly crippled yourself.

Sep 9 '06 #13
Robert J. Hansen wrote:
Clark S. Cox III wrote:
'give it to G++ and see'."
Except that G++ could do it differently each and every time you run the
program.

Yes, it could. And if you try it a few times, you'll discover if it
does.
If you try something on your compiler, you might also find that it
works the same way every time and appears to produce the expected
behavior.

If you know that the behavior is undefined, and you still require this
type of experimentation to convince yourself of something, it suggests
that you don't understand what it means for behavior to be undefined.

Sep 10 '06 #14
If you know that the behavior is undefined, and you still require this
type of experimentation to convince yourself of something, it suggests
that you don't understand what it means for behavior to be undefined.
There's nothing of which I need convincing with respect to undefined
behavior. Anyone who relies on undefined behavior is living in sin.

However, it's a simple fact that failures happen. Failures happen
_lots_. They happen when you write code and they happen when other
people write code which is then dumped in your lap. Understanding how
your compiler acts when pushed out of spec is valuable knowledge in
debugging. Understanding what the symptoms are (or "may be", since
undefined behavior is not required to be reproducible) is a good way to
equip yourself for spotting problems in the future.

As an example, while I would certainly never want to work with a
programmer who deliberately wrote off the end of arrays, I would
certainly never want to work with a programmer who had no idea what
problems a buffer overflow could create, or the symptoms of those
problems.

Sep 10 '06 #15
Robert J. Hansen wrote:
Why not try it for yourself? The great virtue of computer science is
that in many ways it's an experimental discipline, not a purely
theoretical one.
this is Really Bad Advice.

It is really _good_ advice, given that he specifically asked a question
tied to a specific implementation. If he wants to know what happens in
G++ when this is done, the best way is to try it for yourself. It's
not as if the compiler costs a lot of money to download.
this is comp.lang.c++. They don't discuss particular implementations.
Even if the question is confined to G++, my points (which you snipped)
about different versions of the compiler or optimisation levels still
apply. Undefined behaviour is, by definition, undefined.
--
Nick Keighley

Sep 10 '06 #16

Robert J. Hansen wrote:
You *still* won't know for sure. It could be the same 20 times in a row,
and then be different on the 21st.

Then you know it's not "each and every".
You don't know that if you only try it once or twice.
Because finding that out provides you with a false sense of security,
and encourages people to write code that makes unwarranted assumptions.

It only encourages a false sense of security if you're dumb enough to
feel secure in undefined behavior.
Or if you don't realise that the behaviour is undefined.
On the other hand, knowing that
"every time I've done this particular bit of undefined behavior in the
past, the results have been like this" is an incredible help in
debugging when you see that exact same pattern of behavior. It's not a
deterministic help, no, but it's an effective heuristic.
Absolutely true, if, and only if, you know beforehand that the
behaviour is undefined.

Knowing that "every time I've done this particular bit of code, the
results have been like this" is at best incredibly unhelpful and at
worst dangerous in debugging when you see a different pattern of
behaviour if you aren't pre-armed with the knowledge that "this
particular bit of code" has undefined behaviour.

To me, and I suspect to other respondants in this thread, it looked
like the OP was at risk of relying on some undefined behaviour because
he did not realise the behaviour of his code was undefined.

Gavin Deane

Sep 10 '06 #17

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

Similar topics

18
by: Xiangliang Meng | last post by:
Hi. void setValue(int n) { int size = getValueLength(); int buffer_p; if (buffer_p) { ....
2
by: Xiangliang Meng | last post by:
Hi, all. What will happen if a virtual function is declared to be virtual again in a derived class? Any harm? Best Regards, Xiangliang Meng
4
by: Anil | last post by:
Question related to ASP.Net web application with .Net Framework 1.1. I am using ADO.Net and ODBC namespace. When the user clicks the process button I am doing database INSERT/UPDATRE.....
67
by: neilcancer | last post by:
i come from china,and i'm sorry that my english is very poor. now i'm studing data structure and i met some problem about c language. could you tell me what will happen after i use free()? i...
17
by: Ravi | last post by:
void main() { main(); } int main() { main(); }
10
by: Shraddha | last post by:
int *s=(int *)2000; int *p=(int *)1000; printf("%d",p-s);
3
by: vimalankvk80 | last post by:
what will happen, if we forget to return ostream referance ? class A { Private: int _a; int _b;
11
by: active | last post by:
If I install .NET Framework 3.0 what will happen to my VS2005 experience? Will it automatically use 3.0? Will I find new features available? Will the VS doc be updated? Thanks fir any...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.