算术和C. [英] Arithmetic and C

查看:65
本文介绍了算术和C.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有关于C应如何与算术运算相关的规则?

是否总是忽略溢出?它们只是被截断了吗? (如果我有

a 16位无符号短整数

无符号短整数a = 65535,b = 10;是a + b定义?还是a * b?和b -

a?)


再见

解决方案

Massimiliano Alberti写道:< blockquote class =post_quotes>
是否有关于C应该如何运算算术运算的规则?


是。

溢出总是被忽略了吗?它们是否被截断?''


无符号算术就是这样。

(如果我有一个16位无符号短整数
无符号short int a = 65535,b = 10;是a + b定义的?
和a * b?和b - a?)




复杂的部分关于unsigned short,是否受到通常的算术转换的限制。在算术运算中,你不知道它提升的类型。

如果int可以代表unsigned short的所有值,

然后unsigned short被提升为int,

(对于像int这样的签名类型,溢出是未定义的)

否则它将被提升为unsigned。
< br $> b $ b -

pete


>关于unsigned short的复杂部分是

受通常算术转换的约束。在算术运算中,你不知道它提升的类型。
如果int可以表示unsigned short的所有值,那么unsigned short会被提升为int,
(对于像int这样的签名类型,溢出是未定义的)
否则会被提升为unsigned。



但如果你推广两个就不会有溢出unsigned short to a

(更大)int ...如果int不大,那么短裤就不会被推广(我认为)(我认为)这将毫无意义。


--- bye


Massimiliano Alberti写道:

< blockquote class =post_quotes>关于unsigned short的复杂部分是,它受通常的算术转换的约束。在算术运算中,你不知道它提升的类型。
如果int可以表示unsigned short的所有值,那么unsigned short会被提升为int,
(对于像int这样的签名类型,溢出是未定义的)
否则它将被提升为unsigned。


但如果你将两个unsigned short提升为a,则不会有溢出
(更大)int ...如果int不大,那么短裤就不会被提升(我认为)(这将毫无意义)。




如果两个无符号短值转换为int类型,

,一个值为INT_MAX,另一个值为10,

则这两个int值之和是未定义的。


-

pete


Are there rules on how should the C behave with the arithmetic operations?
Are the overflows always ignored? And are they simply truncated? (if I have
a 16-bits unsigned short ints
unsigned short int a = 65535, b = 10; is a + b defined? And a * b? and b -
a?)

bye

解决方案

Massimiliano Alberti wrote:


Are there rules on how should
the C behave with the arithmetic operations?
Yes.
Are the overflows always ignored? And are they simply truncated?''
unsigned arithmetic is like that.
(if I have a 16-bits unsigned short ints
unsigned short int a = 65535, b = 10; is a + b defined?
And a * b? and b - a?)



The complicated part about unsigned short, is that it is
subject to the "usual arithmetic conversions" in arithmetic
operations, and you don''t know what type it promotes to.
If int can represent all of the values of unsigned short,
then unsigned short gets promoted to int,
(overflow is undefined for signed types like int)
otherwise it would get promoted to unsigned.

--
pete


> The complicated part about unsigned short, is that it is

subject to the "usual arithmetic conversions" in arithmetic
operations, and you don''t know what type it promotes to.
If int can represent all of the values of unsigned short,
then unsigned short gets promoted to int,
(overflow is undefined for signed types like int)
otherwise it would get promoted to unsigned.


But there can''t be an overflow if you promote two unsigned shorts to a
(bigger) int... and if the int isn''t bigger then the shorts won''t be
promoted (I think) (it would be pointless).

--- bye


Massimiliano Alberti wrote:

The complicated part about unsigned short, is that it is
subject to the "usual arithmetic conversions" in arithmetic
operations, and you don''t know what type it promotes to.
If int can represent all of the values of unsigned short,
then unsigned short gets promoted to int,
(overflow is undefined for signed types like int)
otherwise it would get promoted to unsigned.


But there can''t be an overflow if you promote two unsigned shorts to a
(bigger) int... and if the int isn''t bigger then the shorts won''t be
promoted (I think) (it would be pointless).



If two unsigned short values are converted to type int,
and one value is INT_MAX and the other is 10,
then the sum of those two int values, is undefined.

--
pete


这篇关于算术和C.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆