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

Liu Hao via cfe-dev cfe-dev at lists.llvm.org
Wed Apr 19 23:27:31 PDT 2017


On 2017/4/20 10:28, Craig Topper via cfe-dev wrote:
> On Wed, Apr 19, 2017 at 7:23 PM, Shiva Chen via llvm-dev
> <llvm-dev at lists.llvm.org <mailto:llvm-dev at lists.llvm.org>> wrote:
>
>     Hi,
>
>     I have a question about unsigned bitwise shift.
>
>     According the C99 6.5.7.4
>
>     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.
Here `E1` is your bit field and is an lvalue, so it is converted to an 
rvalue of type `unsigned long long` (see N1256 6.3.2.1/1), and 
left-shifting it by one yields an rvalue with the value `0x10000000000` 
and the type `unsigned long long`. The rvalue is then converted and 
stored in the bit field. Since the bit field is unsigned and said to 
have a 'pure binary notation' (see N1256 6.2.6.1/2), its value ranges 
from 0 to (2^40-1). So you are right, the result is stored modulo 2^40, 
yielding zero (see N1256 6.3.1.3/2).

-- 
Best regards,
LH_Mouse




More information about the cfe-dev mailing list