Uhhh.. I hope you were going for humor here, right? Otherwise this is a textbook-perfect example of one of your "nonsense" posts that I complained about before.bob wrote:No I really can't figure it out. Actually I already knew the answer. This one was actually handled slightly better by the standards committee. Shifting a negative value right is not undefined behavior. It is implementation dependent. Seems we get way too many claims of UB.Can you REALLY not answer this yourself?bob wrote:BTW, for the divides the compiler in unoptimized mode produces the undefined behavior right shift. Go figure. It is undefined if I do it, but apparently not if the compiler does it. How exactly does that work, again???
Ronald knows the actual answer of course, but in case anyone is still reading along who doesn't know, I will answer your question. (note: I'm not really answering it for bob's benefit, since its abundantly clear by now that the part of his brain where he stores this kind of stuff is set to 'read only'.)
You are not the compiler. Your obligations under the standard and the compiler's obligations under the standard are two completely different things. The compiler's job is to generate x86 machine code that has the standard-described semantics for all of the things that the standard defines (and can do whatever the compiler vendor wants in the cases that the standard says are undefined). The compiler really is writing x86 machine code and can--actually, must--rely on the x86 machine semantics you are so fond of, to meet its obligations according to the standard. YOU, on the other hand, are a mere mortal human C programmer writing C code. This is NOT the "high-level assembler" language you seem to think it is, but a language whose semantics are clearly spelled out in the standard. So YOU don't get to rely on x86 machine semantics for your C code. All the semantics you get is what the language standard promises to give you, or what the compiler vendors decide to add to it themselves. If this isn't enough for you, better switch back to FORTRAN or write the next version of Crafty in assembler so you can take direct advantage of x86 signed integer overflow semantics!bob wrote:It is undefined if I do it, but apparently not if the compiler does it. How exactly does that work, again???
See the difference? The compiler generates machine code. YOU write source code in a language that you think is C. Other programmers like you do the same, with the added bonus that some of them actually understand how to write programs that are actual legal C programs. But you seem to be laboring under the bizarre delusion that your C program's semantics will somehow automatically match the semantics of the x86 machine underneath. A language-spec-defined subset of them does match, but in general, they are two completely different animals that just happen to partly overlap. And they ALWAYS have been, even if you managed to program in C for over 30 years without ever noticing (an impressive feat, by the way! )