A warning. The result of an integer operation is out of range c

I use the RVCT compiler to compile this code in C (corresponding section here):

static void Test (void) { unsigned long regVal; regVal |= (UINT32)( (0x1 << 31) | (0x1 << 26) | 0x3E); } 

When compiling the code, I get the following warning: "# 61-D: the result of the integer operation is out of range."

I would like to understand what to change in order to avoid a warning.

Thank you in advance!

+7
c gcc
source share
2 answers

Due to the integer promotion rules, an internal expression (i.e. before casting (UINT32) ) is considered as a signed int . Therefore, 0x1 << 31 is 0x80000000 , which is a negative integer, which leads to a warning. To fix, you can force the shifts to be unsigned by adding "U" to the hexadecimal constants, for example 0x1U .

  regVal |= (UINT32)( (0x1U << 31) | (0x1U << 26) | 0x3EU); 

This will cause all shifts and bitwise ORs to be unsigned, which should eliminate the warning (and also eliminate the need for a cast (UINT32) , since it is already unsigned).

+7
source share

The compiler overflow warning is correct, because expression 1 <31 is a signed int. To avoid the warning, explicitly define the constant 0x1 as an unsigned value using the U postfix. For example:

 unsigned long regVal; regVal |= (UINT32)( (0x1U << 31) | (0x1 << 26) | 0x3E); 
+2
source share

All Articles