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

Friedman, Eli via llvm-dev llvm-dev at lists.llvm.org
Thu Apr 20 11:03:38 PDT 2017


This discussion got moved to cfe-dev.

-Eli

On 4/20/2017 10:56 AM, Friedman, Eli via llvm-dev wrote:
> On 4/19/2017 7:23 PM, Shiva Chen via llvm-dev 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.
>>
>> 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 6.7.2.1
>> 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 
>> 6.5.7.4.
>
> The type of the field is long long; the bitfield size modifier is not 
> part of the type.  x.b is a 64-bit integer, not a 40-bit integer.
>
> -Eli
>

-- 
Employee of Qualcomm Innovation Center, Inc.
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project



More information about the llvm-dev mailing list