c/c++ left shift unsigned vs signed

后端 未结 5 1814
庸人自扰
庸人自扰 2021-02-14 13:53

I have this code.

#include 

int main()
{
    unsigned long int i = 1U << 31;
    std::cout << i << std::endl;
    unsigned lon         


        
5条回答
  •  青春惊慌失措
    2021-02-14 14:52

    It's not "bizarre".

    Try printing the number in hex and see if it's any more recognizable:

    std::cout << std::hex << i << std::endl;

    And always remember to qualify your literals with "U", "L" and/or "LL" as appropriate:

    http://en.cppreference.com/w/cpp/language/integer_literal

    unsigned long long l1 = 18446744073709550592ull;
    unsigned long long l2 = 18'446'744'073'709'550'592llu;
    unsigned long long l3 = 1844'6744'0737'0955'0592uLL;
    unsigned long long l4 = 184467'440737'0'95505'92LLU;
    

提交回复
热议问题