This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Problem with Shift-Operator

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

}

With Simulator and real hardware n=3!

The Compiler generates a shift of 22, which is invalid. The Assembler Opcode only evaluates the lower 4 bits of the shift-operand.

Generated Assembler Code:
R4 contains the variable n:

 MOV R5, #0x16 // incorrect Opcode???
 SHR R4,R5

The lower 4 bits of 0x16=0x06, and that is what really happens. ( 255 >> 6 = 3 )

I get the same result with Compiler V5.04.

Is it invalid to shift a byte-variable more than 8 times?
If so, why does the compiler not generate a warning?

  • 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.