(int << ulong) == int ?

bearophile bearophileHUGS at lycos.com
Sun Aug 7 17:01:11 PDT 2011


Brad Roberts:

> Which has nothing to do with the question about integral promotion.

The original code was similar to this C code:

int main() {
    unsigned int j = 42;
    unsigned long long k = 1 << (unsigned long long)j;
    return k;
}


If you process it with a simple C lint you get a warning 647:

1  int main() {
2      unsigned int j = 42;
3      unsigned long long k = 1 << (unsigned long long)j;
3  Info 701:  Shift left of signed quantity (int)
3  Warning 647:  Suspicious truncation
4      return k;
4  Info 712:  Loss of precision (return) (unsigned long long to int)
5  } 

I think a compiler able to detect similar cases helps the OP avoid asking the question "I though left operand should be promoted to the largest integer in shift expression, isn't it?".

--------------

Dmitry Olshansky:

> It's useful but not a panacea.

I agree, you need something more/better to improve the situation a lot.

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list