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

variables with type unsigned long problem

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

Parents
  • 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;
    

    Always use the operations on the operands after keeping the data types in mind. - Neo

Reply
  • 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;
    

    Always use the operations on the operands after keeping the data types in mind. - Neo

Children