Pular para o conteúdo principal
Knowledgebase
Home
Renesas Brasil - Knowledgebase

The result of shift operation for a variable (CC-RL)

Last Updated:04/17/2017

Question:

I am using the RL78 compiler. I receive an unexpected value as I describe below from a shift operation for a variable of the unsigned long type. 
Example:
   val = 0x00000001 << x
   (x is 0 to 31)
   When x is 15, the value of the result is 0xFFFF8000, even though I expected the value 0x00008000.
   When x is 16, the value of the result is 0x00000000, even though I expected the value 0x00010000.
   Do I have to use some kind of special code to acquire the value that I expect?

Answer:

Put “UL” as a suffix for integer constants to indicate that they are of the unsigned long type.

If the number has no suffix, 0x00000001 is of the int type, and so will be handled as 16-bit signed. 
When x is 15, the value becomes 0xFFFF8000 because the value 1 has been shifted into the most-significant bit, the sign bit.
When x is 16 or a greater number, the value becomes 0x00000000 because the value is shifted out of the whole 16-bit range.
 

Suitable Products
e² studio
CS+ (formerly CubeSuite+)
C Compiler Package for RL78 Family