Dear All: When I compile the the code with the statement: The type of short_idx is unsigned short. Row1= (short_idx * 64) >> 8 >>8; If the value of short_idx is 2048, the Row1 should be 2,but Row1 will be 0. But if I change the statement to the following. Row1= ((unsigned long)short_idx * (unsigned long)64) >> 8 >>8; Then Row1 would become 2 and is correct. So what should I do in compiler to avoid of such problem without adding "unsigned long" in every statement I used in my problem? Thank you ...
Think about it: multuplying by 64 is equivalent to shifting to the left by 6 bits; therefore, it is very likely that you will "lose" some of the most-significant bits. By using a long instead of a short, you provide some extra space into which the high bits can be shifted without being lost!
Dear Neil: Thanks for your explaination. Your suggestion could solve the problem. In my program, there are still some other statements facing such issue. So I want to find a proper soluction for this problem. Do I miss something else for the issue? And also, I have another problem. I write the program. unsigned long X; X=XBYTE[xx]<<8; Value of XBYTE[xx] is 0x80; and the value of X will be 0xFFFF8000; In my thinking the type of XBYTE is unsigned char and the result should be 0x8000; What do I miss? Thank you .... Best regards ...
You have assigned xbyte as unsigned char and trying to left shift it 8 times. This will cause an unexpected result. You try by type casting the xbyte to unsigned long as,
X = (Unsigned long)XBYTE[XX] << 8;
what are you doing? Row1= (short_idx * 64) >> 8 >>8; the equivalent of << 6 >>8 >>8 why not >>2 >>8 or >>10 Erik