开发者

Cannot (again) assign correct value to signed short

开发者 https://www.devze.com 2023-02-24 04:29 出处:网络
Why this gives zero as an output? I suspect some compile开发者_高级运维r work in it but why? signed int sint_ = numeric_limits<signed int>::min() << \'\\n\';

Why this gives zero as an output? I suspect some compile开发者_高级运维r work in it but why?

  signed int sint_ = numeric_limits<signed int>::min() << '\n';  
    cout << "signed int: " << sint_ << '\n';


This is because of the accidental << '\n' on the first line. Its effect is to left shift the bits of the minimum value by 13 positions (13 being the character code of \n). Since the bit pattern of the most negative value is 1000...0, the result becomes 0.


signed int sint_ = numeric_limits<signed int>::min() << '\n';  

What is this \n at the end?

Is this not what you want:

signed int sint_ = numeric_limits<signed int>::min();

?

Demo : http://ideone.com/aOXGH

0

精彩评论

暂无评论...
验证码 换一张
取 消