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?
look at the code. loop 1 shifts sum 0 bits. loop 2 shifts sum 1 bit. loop 3 shifts sum 2 more bits. and so on. The constant is restored each time. you most likly want sum >> 1
can you please elaborate further. what i want to do is bring the msb 8 bits of the 16 bit value in the variable sum to the lsb position. so i have to shift right 8 times and so the loop is used
sum>>=8
where sum= 32 bit no. what i want to do is bring the msb 8 bits of the 16 bit value in the variable sum to the lsb position.
I didn't catch the information whether the sum is a 32bit or 16bit number.And the result you want to get.
You'd better draw a simple figure to present your problem clearly. Just like: +0 1 2 3 4 5 6 7 8 9 a b c d e f+ | | |
...............
here sum is defined as long int in the code. hence it can have a 32 bit value. but in the present case 'sum' holds a 16 bit value only. the problem is: the instruction for right shift is not executed if the syntax is as follows: for(i=0;i<=8;i++) { j=sum>>i; } but the instruction is executed if the syntax is as follows: for(i=0;i<=8;i++) { j=0x0258>>i; } i.e. the instruction is not executed if i am using the variable name 'sum' but if i put a numerical value instead of 'sum' then it is easily executed. how do i fix this problem?