M
Mr. Ken
Left shift by negative numbers, will I get 1/2?
Thanks.
Thanks.
Mr. Ken said:Left shift by negative numbers, will I get 1/2?
Heinz Ozwirk said: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
Robert J. Hansen said: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.
Robert said: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.
Robert said: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.
Mr. Ken said:Left shift by negative numbers, will I get 1/2?
this is Really Bad Advice.
Robert said: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'."
Clark said:Except that G++ could do it differently each and every time you run the
program.
Robert said: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.
and then be different on the 21st.
I have personally seen all of the following:
Because finding that out provides you with a false sense of security,
and encourages people to write code that makes unwarranted assumptions.
Robert said:Yes, it could. And if you try it a few times, you'll discover if it
does.
type of experimentation to convince yourself of something, it suggests
that you don't understand what it means for behavior to be undefined.
Robert said: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.
Robert said:Then you know it's not "each and every".
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.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.