Question on XC and data type precedence

Technical questions regarding the XTC tools and programming with XMOS.
bearcat
Respected Member
Posts: 283
Joined: Fri Mar 19, 2010 4:49 am

Question on XC and data type precedence

Post by bearcat »

I have the following code line:

ciirsL.B0 = p.B0H << 8 + p.B0L >> 24;
---^-int----------^-int---------------^-unsigned

The structures assign the types as shown above.
The >>24 incorrectly is ASHR instead of SHR for an unsigned in the compiled code.

Doesn't work either:
ciirsL.B0 = p.B0H << 8 + (unsigned)p.B0L >> 24;

Does work:
ciirsL.B0 = p.B0H << 8 + (p.B0L >> 24);

Is this typical for C and I need to recognize this from now on? Is it typical to just enclose everything in C? I would have thought the >> had precedence and been a SHR instead. Lucky for me a caught this very quickly when it didn't work.


richard
Respected Member
Posts: 318
Joined: Tue Dec 15, 2009 12:46 am

Post by richard »

Based on the associativity and precedence of the C / XC operators this is evaluated as:

Code: Select all

ciirsL[i].B0 = (p[i].B0H << (8 + p[i].B0L)) >> 24;
In C / XC if x is of type int then so is x << y. Therefore (p.B0H << (8 + p.B0L)) is of type int and so an arithmetic right shift is used for the shift by 24.