[llvm-dev] Unsigned Bitwise Shift for Bit-field Structure

Shiva Chen via llvm-dev llvm-dev at lists.llvm.org
Wed Apr 19 19:23:13 PDT 2017


I have a question about unsigned bitwise shift.

According the C99

The result of E1 << E2 is E1 left-shifted E2 bit positions; vacated
bits are filled with zeros. If E1 has an unsigned type, the value of
the result is E1 × 2^E2, reduced modulo one more than the maximum
value representable in the result type.

So if

unsigned b = 0x80000000;
unsigned a = b << 1;
a will equal to 0 because a = (b << 1) mod (1<<32);
(1<< 32) is UINT_MAX+1

For the bit-field structure defined as
struct foo
  unsigned long long b:40;
} x;

According to C99
A bit-field is interpreted as a signed or unsigned integer type
consisting of the specified number of bits.
So the x.b will treat as 40 bit unsigned integer and it should follow

if x.b = 0x80000000 00;
x.b << 1 = (x.b << 1) mod (1<<40)
So x.b << 1 should be 0, right ?

Please correct me if I miss understanding something.


More information about the llvm-dev mailing list