c/c++ left shift unsigned vs signed

后端 未结 5 1820
庸人自扰
庸人自扰 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:54

    I think it is compiler dependent .
    It gives same value
    2147483648 2147483648
    on my machiene (g++) .
    Proof : http://ideone.com/cvYzxN

    And if overflow is there , then because uwantsum is unsigned long int and unsigned values are ALWAYS positive , conversion is done from signed to unsigned by using (uwantsum)%2^64 .

    Hope this helps !

提交回复
热议问题