When to use different integer types?

前端 未结 8 1189
猫巷女王i
猫巷女王i 2021-02-13 20:54

Programming languages (e.g. c, c++, and java) usually have several types for integer arithmetic:

  • signed and unsigned types
  • types
8条回答
  •  后悔当初
    2021-02-13 21:10

    Maybe just for fun, here you have a simple example showing how according to what type you choose you have one result or an other.

    Naturally the actual reason why you would choose one type or an other, in my opinion, is related to other factors. For instance the great shift operator.

    #include 
    #include 
    using namespace std;
    
    int main()
    {
        int i;
    
        //unsigned long long x;
        //int x;
        short x;
    
        x = 2;
        for (i=2; i<15; ++i)
        {
            x=pow(x,2);
            cout << x << endl;
        }
        return 0;
    }
    

提交回复
热议问题