[cfe-dev] [llvm-dev] Unsigned Bitwise Shift for Bit-field Structure
Craig Topper via cfe-dev
cfe-dev at lists.llvm.org
Wed Apr 19 19:35:23 PDT 2017
This test program prints 0.
#include <stdio.h>
struct foo
{
unsigned long long b:40;
} x;
int main() {
foo f;
f.b = 0x8000000000ULL;
f.b <<= 1;
printf("%llx\n", f.b);
return 0;
}
~Craig
On Wed, Apr 19, 2017 at 7:28 PM, Craig Topper <craig.topper at gmail.com>
wrote:
> Adding the clang developer list and removing the llvm developer list.
> Clang folks would be better for asking a C++ question such as this.
>
> ~Craig
>
> On Wed, Apr 19, 2017 at 7:23 PM, Shiva Chen via llvm-dev <
> 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.
>>
>> 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.
>>
>> 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.
>>
>> Thanks,
>> Shiva
>> _______________________________________________
>> LLVM Developers mailing list
>> llvm-dev at lists.llvm.org
>> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20170419/e88efed2/attachment.html>
More information about the cfe-dev
mailing list