This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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


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.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]