X86_64 bit shifts

Wachdorf, Daniel R drwachd@sandia.gov
Fri Jul 6 12:07:00 GMT 2007


I am seeing an odd behavior and was wondering if it was a bug.  In
X86_64, Gcc is sign extending a bit shift operation when assigned to an
unsigned long.  Specifically:

#include <stdio.h>

int main(){

    unsigned long val;

    val = (1 << 31);

    printf("Val = %lx\n", val);

    return 0;
}

This results in:

Val = 0xffffffff80000000;

Should the result be 0x80000000?  I understand that the bit shift is a
32 bit operation, but shouldn't the compiler then up convert that to a
64 bit unsigned long?

Thanks.

-dan



More information about the Gcc mailing list