We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Hi, I have a Problem with the Shift-Operator >> within my C-Code. I'm using PK166 V4.2 The following code should set the Variable n to 0.
void main() { unsigned char n; unsigned char p; // dummy n=255; n=n>>22; p=0; // dummy }
MOV R5, #0x16 // incorrect Opcode??? SHR R4,R5
"It is, of course, possible that the standard has been updated, but my copy of the ANSI from years back state: "If the shift count is negative or is greater than or equal to the number of bits contained in the object being shifted, the result of the shift is undefined"." Here we go again. Try reading the posts rather that replying blind. There is a difference between a 'negative shift count' and 'shifting a negative number'.
The Standard specifies that when shifting right an object having a signed type with a negative value, the resulting value is implementation-defined. It would be nice if that was CLEAR. if I shift something with a value of x that means to me that I am shifting x places. Erik
Oh, come on, Erik, you surely know better than to believe everything being said about either operand of a two-operand operator like '<<' would automatically apply to both of them. It can't be all that hard to distinguish between the number being shifted, and the number of positions it's shifted by, now can it? Negative shift counts are strictly forbidden. Shifts of negative values are implementation-defined. Which, as I said earlier, makes them risky. It would be nice if that was CLEAR. Being unmistakably clear is about the only thing a standard is required to be at all, besides not directly contradicting itself. I would say the C99 standard is quite clear indeed. It takes a while to get used to the jargon, but that's unavoidable in a text like this, which is effectively a legal and a technical document at the same time.
"The Standard specifies that when shifting right an object having a signed type with a negative value, the resulting value is implementation-defined. It would be nice if that was CLEAR." Seems perfectly clear to to me. The standard goes to great lengths to write unambiguous English. "if I shift something with a value of x that means to me that I am shifting x places." Ludicrous: 'Shift y right with x bits' 'Shift y right by x bits'.
I am so happy that you found an opportunity to critisize me again, the wait must have been terrible. I'll let the readers decide who is "ludicrous". Erik
"if I shift something with a value of x that means to me that I am shifting x places." And for right-shifts, that works as expected when the value being shifted is unsigned or signed and is positive. It is up to the compiler implementer how to handle a signed negative value's sign bit when shifting right. The implementer is expected to handle it in a documented, defined manner of their choice.
"I am so happy that you found an opportunity to critisize me again, the wait must have been terrible." I have no desire to criticise you. If you were to spend a little more time reading and understanding things there would be no need. "I'll let the readers decide who is "ludicrous"." It seems they already have.