I am facing a problem in using the right shift operator in C with keil. The instruction is:for(i=0;i<=8;i++) { j=sum>>i; } where sum= 32 bit no. the instruction is not executed if the syntax is as shown above. but if i write a numerical value instead of using the variable name 'sum' then the instruction is easily executed. i need to use the variable name only. how do i fix this problem?
the code is for adding 15 nos and then finding the average. the problem i am facing is: if the synax is as follows:
j=sum>>8;
the instruction is not executed. but if i put a numerical value instead of the variable name sum then the instruction is easily executed. Similarly if the syntax is as follows:
avg=sum / 0x0f;
the above instruction is not executed using the variable name 'sum'. Since i need to use only variable names in the code what can i do about the above mentioned problems.
"Since i need to use only variable names in the code"
Why is that?
Despite many requests, you still have not explained why you need to do this shift in a loop, rather than simply
j = sum >> 8;
"the above instruction is not executed"
How have you confirmed that it is actually "not executed"?
Have you examined the generated code?
Have you stepped through it in the simulator?
"the above instruction is not executed using the variable name 'sum'"
Just a thought: have you checked that 'sum' hasn't been defined with some special meaning, etc...
What if you call it 'my_sum' or something?
yes i have confirmed that it is not executed. yes i have examined the generated code. i did single stepping and found that these instructions are not executed.
and i have done away with the for loop and instead just used:
yeah i have checked that too. even if i replace 'sum' by any other varible name say 's' or 'x' or whatever, it is still not executed
"yes i have examined the generated code."
And what did you observe?
"i did single stepping and found that these instructions are not executed."
Again, what did you observe?
This should tell you why they weren't executed!
"i have done away with the for loop and instead just used..."
Again, you still haven't explained why you wanted the for loop in the first place!
The 'for' loop is to right shift the contents of the variable 'sum'. 'sum' holds my 16 bit result. now i want to move this result in two 8 bits registers of the microcontroller. for that moving the lsb 8 bits of the result is simple. but the msb 8 bits have to be brought to the lsb position first to move it into an 8 bit reg. hence i used the for loop. but now as per your advice i have done away with the for loop and simply used:
But still the instruction is not executed. is there any other alternate way of moving the 16 bit result in 'sum' to the registers of the microcontroller?