[Bug c/53013] New: Inconsistent Behaviour with Left Shift Operator.

john.stevens at f5 dot com gcc-bugzilla@gcc.gnu.org
Mon Apr 16 21:01:00 GMT 2012


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53013

             Bug #: 53013
           Summary: Inconsistent Behaviour with Left Shift Operator.
    Classification: Unclassified
           Product: gcc
           Version: 4.4.5
            Status: UNCONFIRMED
          Severity: minor
          Priority: P3
         Component: c
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: john.stevens@f5.com


In this example:

#include    <stdio.h>
#include    <stdlib.h>
#include    <stdint.h>

#define     ALL_KEY_BITS    (~ (uint32_t) 0)

int
main(
    int     argc,
    char    **argv )
{
    register    uint32_t    i;

    printf(
        "Size: %d\n",
        sizeof( 32LU ) );
    printf(
        "Mask (<< %lu): 0x%lx\n",
        32LU,
        ALL_KEY_BITS << 32LU );
    i = 32LU;
    printf(
        "Mask (<< %lu): 0x%lx\n",
        i,
        ALL_KEY_BITS << i );
}

The result is:

Size: 4
Mask (<< 32): 0x0
Mask (<< 32): 0xffffffff

While the above code is undefined by the standard, it would be preferable to
have consistent behaviour between shifting by a constant, or by a variable.

A result of all zero would give the least surprising result; if you start with
an unsigned value with only the top bit set, and shift it left by one, the
result is zero.  Start with one, shift it left by one in a loop where the
iteration count is the number of bits in the data, you get zero.



More information about the Gcc-bugs mailing list