积分促销,算术转换,保值,无符号保留??? [英] integral promotion, arithmetic conversion, value preserving, unsigned preserving???

查看:73
本文介绍了积分促销,算术转换,保值,无符号保留???的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我对C来说比较新,并且已经阅读了超过4本书

。没有提到有关积分推广,算术转换,保值和无符号保留的任何内容。并且K& R2

提及已签名的扩展名无处不在。


阅读一些旧的clc帖子,我已经开始意识到这些书

过分概括了这个话题。我只是想知道以下两对术语之间的差异是什么:


1)整体推广与算术转换

2)保值与未签约保留


我已经阅读了很多很多来自clc的旧帖子,这些帖子经常提到这些4

条款。这就是我认为他们的意思:


积分促销

===================

在对它们进行任何操作之前,所有字符和短裤都会提升为int(签名)。如果unsigned short与int具有相同的位大小,则

然后促销为unsigned int。


整体促销是作为中间步骤完成的所有常见的

计算。


Ex:


char a = 9;

unsigned b = 4;


a = b + a; / * a和b都在加入之前转换为int * /

/ *结果然后通过赋值转换回char * /


所以基本上,整体推广保证,所有中间的计算都没有处理低。比一个int。我是对的吗?


-

Artihmetic促销

=============== ======

如果算术运算中涉及的变量都至少为
int,那么level优先接管。


优先级为:long double,double,float,long,int


如果一个变量涉及二进制操作是一个更高的水平比另一个更好,另一个是另一个,另一个是升级到同一级别。


之后,还会应用下一种优先级,基于

签名/未签名。

如果一个变量是未签名的而另一个是签名的,则签名是

转换为无符号。

完成所有这些后,我们继续进行二进制操作,以产生结果的
。我是对的吗?


-

价值保留

=============== =

我对这个词非常困惑。这是否意味着,例如,从无符号短路转换为int的

转换,存储在无符号短路中的值

大于最大值可能

一个int中的正数,然后结果是实现定义的。

如果存储在unsigned short中的值小于最大值

in中的正数,然后确切的值将退出

int(保证msb = 0)。


我不喜欢什么得到保值规则是,它们是基于变量的实际值(运行时间)是否为
,或者是INTEGERS的可能的
范围(编译时) ?


-

无符号保留

================== =

我有一种感觉这是一个更严格的规则(值的子集

保留)意味着2'的恭维。实际的位图是

在没有变化的情况下移动,就像2'的恭维一样。

-


关于这些主题的许多帖子已经通过了clc的服务器,但是他们都处理了我在这里展示的子集。如果某人

可以做出更全面的回复(或者可以将其添加到FAQ

网站),我认为这将是一个重要的帮助(特别是当书籍

只提及已签名的延期..有些人没有提及任何事情......他们只需要说价值被转换......看看C89草案......对于一个初学者来说,这是一个很高的




那么,如果你们其中一个clc常客在C上写一本书就像
那样彻底
您给出的回复,它会迫使所有这些虚假的作者

濒临灭绝。


[我工作场所的许多程序员(其中)我是一个电子产品的人)

当我问他们这个问题时画一个空白:-(]

解决方案

" TTroy"< ti ***** @ gmail.com>写道:

整体推广
========= ==========
所有字符和短片都被提升为int(签名)b在任何操作之前完成它们。如果unsigned short与int具有相同的位大小,那么促销就是unsigned int。


同意无符号字符。不过,这是一个罕见的场合,无论是在b / b
实现之外。

char a = 9;
无符号b = 4;

a = b + a; / * a和b都在添加之前转换为int * /
/ *结果然后通过赋值转换回char * /

所以基本上,整体推广保证所有中间
计算没有任何低处理。比一个int。我对吗?


是的。不幸的是 - 我不喜欢这个功能。

Artihmetic Promotion
===================== <如果算术运算中涉及的变量都至少为int,那么level和优先权接管。

优先级是:long double,double,float,long,int
如果二进制操作中涉及的一个变量是更高的级别。而另一方面,另一方被提升到同一水平。

之后,还会根据
签名/未签名的方式应用下一类优先级。
如果一个变量是无符号的而另一个是有符号的,则签名将被转换为无符号。

一旦完成所有这些,我们继续使用二进制操作来生成
结果。我是对的吗?




一半。你写的是正确的;但是对于浮点类型也有类似的促销



Richard



Richard Bos写道:

" TTroy" < TI ***** @ gmail.com>写道:

积分促销
===================
所有字符和短裤都被提升为int(签名)在任何
操作之前完成。如果unsigned short与
int具有相同的位大小,那么促销将是unsigned int。



同样对于unsigned char。不过,这是在独立式实施之外的罕见场合。




积分促销


char或a只要

可以使用整数,就可以在表达式中使用short(有符号或无符号)。如果int可以表示

原始类型的所有值,则该值将转换为int;否则值

将转换为unsigned int。


=>要添加到OP:当完成此转换时,一次为促销考虑多少个整数

变量,并且

究竟是什么表达?意味着所有解释都缺乏。


例如(s​​izeof(短)== 32,sizeof(int)== 32):


unsigned short a = -1;

签名短b = 45;

签名char c = -34;

unsigned char d = 21 ;

签名长f = SLONG_MAX;

int = e;


e =(a * b + c * d)* f;

是否所有a,b,c,d都被认为是

一次的整体促销规则?或者是& b被视为c& d的独立促销?在任何进一步的

转换(基于arirthmetic规则)之前,整个表达式中的所有

积分促销都已完成吗?这些是我在K& R2或

标准中没有找到启发的一些问题(没有足够的经验来理解标准) 。


Kobu写道:

Richard Bos写道:

" TTroy" < TI ***** @ gmail.com>写道:

积分促销
===================
所有字符和短裤都被提升为int(签名)在任何
操作完成之前。如果unsigned short与int具有相同的位大小,则促销为unsigned int。



No.标准说...


"如果int可以表示原始类型的所有值,则

值将转换为int;否则,它将转换为

unsigned int。


位大小无关紧要。这是_value_范围很重要。

同意无符号字符。然而,这是一种罕见的独立
实施之外的场合。



托管实现是否可论证

INT_MAX< UCHAR_MAX可能符合要求。

积分促销

只要可以使用整数,就可以在表达式中使用字符或短字符(无论是否签名)
。如果int可以表示原始类型的所有值,则该值将转换为int;
否则该值将转换为unsigned int。

=>要添加到OP中:当完成此转换时,一次要考虑多少
积分变量进行促销,以及确切地说表达是什么?手段是所有
解释所缺乏的。

例如(sizeof(短)== 32,sizeof(int)== 32):


你有一台32字节整数的机器!无论大小如何,

整数类型的范围都受到限制,但_not_确定

整数的大小。


表达式......


sizeof(短)> sizeof(int)


....理论上可以在假设的符合

实现中评估为1.

unsigned short a = -1;
签名短b = 45;
签名char c = -34;
unsigned char d = 21;
签名长f = SLONG_MAX;


如果你的意思是LONG_MAX,后面的表达式就是一个坏的例子

,因为溢出会导致未定义的行为。

int = Ë;


ITYM int e;

e =(a * b + c * d)* f;

都是, b,c,d一次被认为反对整体推广规则?


是。

或是& b被视为c& d的独立促销?在进行任何进一步的转换之前(基于arirthmetic规则),是否所有
积分促销都已完成?




实际上,是的。虽然实现可以在幕后以各种方式实现这一点。 [例如,对于上面的

表达式,给定的编译器可以在执行任何算术运算之前将所有参数

静默转换为long,如果它可以

确定此行为不会改变所需的结果。]


-

彼得


Hello, I''m relatively new to C and have gone through more than 4 books
on it. None mentioned anything about integral promotion, arithmetic
conversion, value preserving and unsigned preserving. And K&R2
mentions "signed extension" everywhere.

Reading some old clc posts, I''ve beginning to realize that these books
are over-generalizing the topic. I am just wondering what the
difference between the following pairs of terms are:

1) Integral promotion vs. Arithmetic Conversion
2) Value Preserving vs. Unsigned Preserving

I''ve read many, many old posts from c.l.c that referred to those 4
terms constantly.. this is what I think they mean:

Integral Promotion
===================
All chars and shorts are promoted to int(signed) before any operating
is done on them. If an unsigned short has the same bit-size as int,
then the promotion is to unsigned int.

Integral promotion is done as an intermediate step for all common
computations.

Ex:

char a = 9;
unsigned b = 4;

a = b + a; /* a and b both converted to int before addition */
/* the result is then converted back to char by the assignment */

So basically, integral promotion gurantees that all intermediate
computations are dealt with nothing "lower" than an int. Am I right?

-
Artihmetic Promotion
=====================
If the variables involved in an arithmetic operation are all at least
int, then "level" priorities take over.

The priority is : long double, double, float, long, int

If one variable involved in a binary operation is a higher "level" than
the other, the other is promoted to that same level.

After that, the next type of priority is also applied, based on
signed/unsigned.
If one variable is unsigned and the other is signed, the signed is
converted to unsigned.

Once all this is done, we go on with the binary operation to produce
the result. Am I right?

-
Value Preserving
================
I am very confused on this term. Does it mean that, say for example, a
conversion from an unsigned short to an int is done, and the value
stored in the unsigned short is larger than the maximum possible
positive number in an int, then the result is implementation defined.
If the value stored in unsigned short is less than the maximum
posssible positive number in an in, then the exact value will exit in
the int (guaranteed to have msb = 0).

What I don''t get about value preserving rules is, whether they are
based on the actual value in variables(run time), or the POSSIBLE
RANGES of the INTEGERS(compile time)?

-
Unsigned Preserving
===================
I have a feeling this is a more strict rule (subset of Value
preserving) that implies 2''s compliment. Where actual bit patters are
moved without change, as would be the case for 2''s compliment.
-

Many many posts on these subjects have gone through clc''s servers, but
they all deal with subsets of what I''m presenting here. If someone
could make a more comprehensive response (or possible add it to the FAQ
website), I think that would be a major help (especially when books
only mention "signed extension".. some don''t mention anything.. they
just say "the value is converted"... looked at C89 draft.. little too
advanced for a beginner).

By the way, if one of you clc regulars write a book on C as thorough as
the responses you give, it would force all these phony book authors
into extinction.

[Many of the programmers at my workplace (where I''m an electronics guy)
draw a blank when I ask them about this :-( ]

解决方案

"TTroy" <ti*****@gmail.com> wrote:

Integral Promotion
===================
All chars and shorts are promoted to int(signed) before any operating
is done on them. If an unsigned short has the same bit-size as int,
then the promotion is to unsigned int.
Ditto for unsigned char. It''s a rare occasion outside freestanding
implementations, though.
char a = 9;
unsigned b = 4;

a = b + a; /* a and b both converted to int before addition */
/* the result is then converted back to char by the assignment */

So basically, integral promotion gurantees that all intermediate
computations are dealt with nothing "lower" than an int. Am I right?
Yes. Unfortunately - I don''t like this "feature".
Artihmetic Promotion
=====================
If the variables involved in an arithmetic operation are all at least
int, then "level" priorities take over.

The priority is : long double, double, float, long, int

If one variable involved in a binary operation is a higher "level" than
the other, the other is promoted to that same level.

After that, the next type of priority is also applied, based on
signed/unsigned.
If one variable is unsigned and the other is signed, the signed is
converted to unsigned.

Once all this is done, we go on with the binary operation to produce
the result. Am I right?



Half. What you''ve written is correct; but there are similar promotions
for the floating point types.

Richard



Richard Bos wrote:

"TTroy" <ti*****@gmail.com> wrote:

Integral Promotion
===================
All chars and shorts are promoted to int(signed) before any operating is done on them. If an unsigned short has the same bit-size as int, then the promotion is to unsigned int.



Ditto for unsigned char. It''s a rare occasion outside freestanding
implementations, though.



Integral Promotion

A char or a short (signed or not) may be used in an expression whenever
an integer may be used. If an int can represent all the values of the
original type, then the value is converted to int; otherwise the value
is converted to unsigned int.

=> To add to the OP: When this conversion is done, how many integral
variables are taken into consideration for the promotion at a time, and
what exactly "expression" means is what all explanations lack.

For example (sizeof(short)== 32, sizeof(int) == 32):

unsigned short a = -1;
signed short b = 45;
signed char c = -34;
unsigned char d = 21;
signed long f = SLONG_MAX;
int = e;

e = (a*b + c*d) * f;
Are all a,b,c,d considered against the intergral promotion rule at
once? Or is a&b treated as an independent promotion from c&d? Are all
integral promotions in the whole expression finished before any further
conversions (based on arirthmetic rules)? These are some of the
questions for which I haven''t found enlightenment in K&R2 or the
standards (not experienced enough to make much sense of the standards).


Kobu wrote:

Richard Bos wrote:

"TTroy" <ti*****@gmail.com> wrote:

Integral Promotion
===================
All chars and shorts are promoted to int(signed) before any
operating is done on them. If an unsigned short has the same
bit-size as int, then the promotion is to unsigned int.


No. The standard says...

"If an int can represent all values of the original type, the
value is converted to an int; otherwise, it is converted to an
unsigned int."

Bit size is irrelevant. It''s the _value_ range that is significant.
Ditto for unsigned char. It''s a rare occasion outside freestanding
implementations, though.


It''s arguable whether a hosted implementation where
INT_MAX < UCHAR_MAX could ever be conforming.
Integral Promotion

A char or a short (signed or not) may be used in an expression
whenever an integer may be used. If an int can represent all the
values of the original type, then the value is converted to int;
otherwise the value is converted to unsigned int.

=> To add to the OP: When this conversion is done, how many
integral variables are taken into consideration for the promotion
at a time, and what exactly "expression" means is what all
explanations lack.

For example (sizeof(short)== 32, sizeof(int) == 32):
You have a machine with 32 byte integers! Whatever the size,
the range of integer types is constrained, but _not_ determined
by the size of the integer.

The expression...

sizeof(short) > sizeof(int)

....can theoretically evaluate to 1 on a hypothetical conforming
implementation.
unsigned short a = -1;
signed short b = 45;
signed char c = -34;
unsigned char d = 21;
signed long f = SLONG_MAX;
If you meant LONG_MAX, the later expression is a bad example
since the overflow results in undefined behaviour.
int = e;
ITYM int e;

e = (a*b + c*d) * f;

Are all a,b,c,d considered against the intergral promotion rule at
once?
Yes.
Or is a&b treated as an independent promotion from c&d? Are all
integral promotions in the whole expression finished before any
further conversions (based on arirthmetic rules)?



Effectively, yes. Though an implementation can achieve this in
various ways behind the scenes. [For instance, for the above
expression, a given compiler may silently convert all the arguments
to long before performing any arithmetic operation, if it can
determine that this act won''t change a required result.]

--
Peter


这篇关于积分促销,算术转换,保值,无符号保留???的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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