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
Is it invalid to shift a byte-variable more than 8 times? From the ANSI C standard: "If the shift count is negative or is greater than or equal to the number of bits contained in the object being shifted, the resulf of the shift is undefined" If so, why does the compiler not generate a warning? The purpose of the compiler is to compile your program, not to protect you against yourself. As you see the compilere adhere to the standard. If you want checking to this level I suggest you obtain a copy of PClint. Erik
Note that Keil EC++ catches that kind of mistake too (it throws a warning). But PC lint is even better! Steph-
"Is it invalid to shift a byte-variable more than 8 times? If so, why does the compiler not generate a warning?" Whatever, it is clearly a very silly thing to try! How did you stumble across this behaviour?
How did you stumble across this behaviour? For debug-purpose I have written a Marcro that puts a variable (8bit, 16bit, 32bit) into a CAN-Message
debug_out(val) data[0]=(val>>24)&0xFF; data[1]=(val>>16)&0xFF; data[2]=(val>>8)&0xFF data[3]=val&0xFF; CANWRITE();
data[0]=(unsigned long) (val>>24); data[1]... ...
"I thought that a right-shift would always insert a 0 at the left side and the result would therefore be 0." Oh well - so you learned something new, anyway!
Sorry, of course I mean
data[0]= (unsigned long)val>>24;