加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 存储负数

发布时间:2020-12-16 03:25:03 所属栏目:百科 来源:网络整理
导读:在下面的代码中 int main(){int a = -1;printf("%d",a1);return 0;} 为什么给出输出-1. 解决方法 位移仅在无符号类型上定义,对于被实现定义的签名类型. 这是一个有用的改进. Strictly speaking,it is defined for signed types whenever the value is positi
在下面的代码中
int main()
{
int  a = -1;
printf("%d",a>>1);
return 0;
}

为什么给出输出-1.

解决方法

位移仅在无符号类型上定义,对于被实现定义的签名类型.
这是一个有用的改进.

Strictly speaking,it is defined for
signed types whenever the value is
positive and the result does not
overflow,and right shift is
implementation-defined for negative
values. Left shift,on the other hand,
is undefined for negative values

┌───┬──────────────┬──────────────────────────────────┬────────────────────────┐
│   │ Unsigned     │ Signed,positive                 │ Signed,negative       │
├───┼──────────────┼──────────────────────────────────┼────────────────────────┤
│<< │ well-defined │ well-defined,except on overflow │ undefined behaviour    │
│>> │ well-defined │ well-defined                     │ implementation-defined │
└───┴──────────────┴──────────────────────────────────┴────────────────────────┘

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读