I’m working with gcc 4.4.5, and have some difficulties in understanding the right shift operator on plain simple unsigned values…
This test
ASSERT_EQ( 0u, (unsigned long)(0xffffffff) >> (4*8) );
passes.
This test
unsigned long address = 0xffffffff;
ASSERT_EQ( 0u, address >> (4*8) );
fails:
Value of: address >> (4*8)
Actual: 4294967295
Expected: 0u
It seems that the variable is treated like a signed value, and thus results in sign-extension. (0xffffffff is 4294967295 in decimal).
Can anyone spot the difference?
It is undefined behaviour to shift a value greater than or equal the size in bits of the left operand (§5.8¶1). (I assume
unsigned longis 32 bits from your comments about 0xfffffff being the expected result if you consider sign extension.)Still, there’s probably something that
ASSERT_EQdoes that causes the difference, as it works fine on GCC 4.5 with good oldassert.