The following code compiled with GCC 4.4 and -O1 produces a wrong result for
the SHIFT and AND operation. Bit 31 of the variable 'var' in fucntion shiftTest
computes to '1' instead of a '0'.

Compiling with -O0 however, produces the right result.


#include "stdio.h"

typedef unsigned long long ulonglong;

int shiftTest (const ulonglong var)
{
  ulonglong predicate = (var >> 31ULL) & 1ULL;

  if (predicate == 0ULL)
    {
      return 0;
    }
  return -1;
}


int main (void)
{
  ulonglong var = 0x1682a9aaaULL;

  printf ("Bit 31 of 0x%llx is %llu\n", var, (var >> 31ULL) & 1ULL);

  int result = shiftTest (var);

  if (result == 0)
    {
      printf ("Bit 31 is 0 - Correct!\n");
    }
  else
    {
      printf ("Bit 31 is 1 - Incorrect!\n");
    }
  return 0;
}


-- 
           Summary: Incorrect right shift by 31 with long long
           Product: gcc
           Version: 4.4.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: rahul at icerasemi dot com
 GCC build triplet: i686-pc-linux-gnu
  GCC host triplet: i686-pc-linux-gnu
GCC target triplet: i686-pc-linux-gnu


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

Reply via email to