类型转换问题 [英] A Type Conversion Question

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

问题描述

考虑以下代码:


#include" dsptypes.h"


/ * definitions * /
< br $>
#define VECTOR_LENGTH 64

/ *本地变量* /


/ *本地函数原型* /


/ *函数定义* /


int main(int margc,char ** margv)

{

UINT16_T n;

INT16_T x [VECTOR_LENGTH];

INT16_T y [VECTOR_LENGTH];

INT32_T acc;

INT16_T结果;


acc = 0;

for(n = 0; n< VECTOR_LENGTH; n ++)

{

x [n] = n;

y [n] = VECTOR_LENGTH - n - 1;

}


acc = 0;

for(n = 0; n< VECTOR_LENGTH; n ++)

{

acc + = x [n] * y [n];

}


result =(INT16_T)(acc>> 16);


返回结果;

}


ISO C如何说明
中的转换如何

acc + = x [n] * y [n];


行是否要完成?具体来说,是乘以16

位,然后将结果提升为32位,或者首先将x和y提升为32位之前的
乘?标准是否指定?

-

Randy Yates

索尼爱立信移动通信

北卡罗莱纳州三角研究园,美国
ra*********@sonyericsson.com ,919- 472-1124

解决方案




Randy Yates写道:

考虑以下代码:

#include" dsptypes.h"


很高兴您隐藏关键细节

没人能看到它们......

/ * definitions * /

#define VECTOR_LENGTH 64

/ *局部变量* /

/ *本地函数原型* /

/ *函数定义* /
int main(int margc,char ** margv)
{UINT16_T n;
INT16_T x [VECTOR_LENGTH];
INT16_T y [VECTOR_LENGTH];
INT32_T acc;
INT16_T结果;

acc = 0;
for(n = 0; n< VECTOR_LENGTH; n ++)
{
x [n] = n;
y [n] = VECTOR_LENGTH - n - 1;
}

acc = 0;
for(n = 0; n< VECTOR_LENGTH; n ++)
{
acc + = x [n] * y [n];
}

结果= (INT16_T)(acc>> 16);

返回结果;


ISO C如何说明

acc + = x [n] * y [n];

线路要做什么?具体来说,是16位的乘法,然后结果提升到32位,或者x和y首先在乘法之前被提升为32位?标准是否指定?




无法提供所提供的信息:你

没有透露INT16_T和INT32_T是什么。


如果INT16_T为short且INT32_T为int,则

两个数组元素将被提升为int,乘以

生成一个32位产品,并添加到`acc''。


如果INT16_T是'int''且INT32_T是'long'',那么

两个数组元素未被提升,乘法

产生一个16位产品,最后产品是

提升为long并添加到'acc''。


如果......啊,pfui。我厌倦了可能的组合贯穿所有

;看看他们自己。


-
Er * ********@sun.com


Eric Sosman< er ********* @ sun.com>写道:

Randy Yates写道:

请考虑以下代码:

#include" dsptypes.h"


很高兴您能隐藏关键细节,而没人能看到它们......

/ * definitions * /

#define VECTOR_LENGTH 64

/ *局部变量* /

/ *本地函数原型* /

/ *函数定义* /

int main(int margc,char ** margv)
{UINT16_T n;
INT16_T x [VECTOR_LENGTH];
INT16_T y [VECTOR_LENGTH ];
INT32_T acc;
INT16_T结果;

acc = 0;
for(n = 0; n< VECTOR_LENGTH; n ++)
{
x [n] = n;
y [n] = VECTOR_LENGTH - n - 1;
}

acc = 0;
for(n = 0; n< VECTOR_LENGTH; n ++)
{
acc + = x [n] * y [n];
}

结果=(INT16_T) ( acc>> 16);

返回结果;


ISO C如何说明如何转换

acc + = x [n] * y [n];

线路要做什么?具体来说,是16位的乘法,然后结果提升到32位,或者x和y首先在乘法之前被提升为32位?标准是否指定?



无法提供所提供的信息:您没有透露INT16_T和INT32_T是什么。

如果INT16_T是`short''和INT32_T是`int'',
两个数组元素被提升为`int'',乘以
产生一个32位的产品,并添加到`acc''。 br />
如果INT16_T是'int''且INT32_T是'long'',则不会提升两个数组元素,乘法
产生一个16位乘积,最后是产品被提升为长并添加到acc中。

如果......啊,pfui。我厌倦了贯穿所有可能的组合;看看你自己。




很高兴,但我没有规格,我明白这是

不便宜。


以下是相关的typedef:


typedef short INT16_T;

typedef long INT32_T; < br $>
-

Randy Yates

索尼爱立信移动通信

美国北卡罗来纳州三角研究园
ra*********@sonyericsson.com ,919-472-1124





Randy Yates写道:

Eric Sosman< er ********* @ sun.com>写道:

如果......啊,pfui。我厌倦了贯穿所有可能的组合;看看他们自己。
很高兴,但我没有规格,我明白它不便宜。




18美元,美国一本教科书可能会花费更多的b $ b,但看起来这对你来说可能是更好的投资。没有冒犯意味着:每个人都从零开始。

以下是相关的typedef:

typedef短INT16_T;
typedef long INT32_T;




长+ =短*短

- > long + =(int)short *(int)short

- > long + = int

- > long + =(long)int

- >长+ =长


-
Er **** *****@sun.com


Consider the following code:

#include "dsptypes.h"

/* definitions */

#define VECTOR_LENGTH 64

/* local variables */

/* local function prototypes */

/* function definitions */

int main(int margc, char **margv)
{
UINT16_T n;
INT16_T x[VECTOR_LENGTH];
INT16_T y[VECTOR_LENGTH];
INT32_T acc;
INT16_T result;

acc = 0;
for (n = 0; n < VECTOR_LENGTH; n++)
{
x[n] = n;
y[n] = VECTOR_LENGTH - n - 1;
}

acc = 0;
for (n = 0; n < VECTOR_LENGTH; n++)
{
acc += x[n] * y[n];
}

result = (INT16_T)(acc >> 16);

return result;
}

What does ISO C say about how the conversions in the

acc += x[n] * y[n];

line are to be done? Specifically, is the multiply to be done with 16
bits, then the result promoted to 32 bits, or are x and y first to be
promoted to 32 bits before the multiply? Does the standard specify?
--
Randy Yates
Sony Ericsson Mobile Communications
Research Triangle Park, NC, USA
ra*********@sonyericsson.com, 919-472-1124

解决方案



Randy Yates wrote:

Consider the following code:

#include "dsptypes.h"
Nice of you to hide the crucial details where
nobody can see them ...
/* definitions */

#define VECTOR_LENGTH 64

/* local variables */

/* local function prototypes */

/* function definitions */

int main(int margc, char **margv)
{
UINT16_T n;
INT16_T x[VECTOR_LENGTH];
INT16_T y[VECTOR_LENGTH];
INT32_T acc;
INT16_T result;

acc = 0;
for (n = 0; n < VECTOR_LENGTH; n++)
{
x[n] = n;
y[n] = VECTOR_LENGTH - n - 1;
}

acc = 0;
for (n = 0; n < VECTOR_LENGTH; n++)
{
acc += x[n] * y[n];
}

result = (INT16_T)(acc >> 16);

return result;
}

What does ISO C say about how the conversions in the

acc += x[n] * y[n];

line are to be done? Specifically, is the multiply to be done with 16
bits, then the result promoted to 32 bits, or are x and y first to be
promoted to 32 bits before the multiply? Does the standard specify?



Unanswerable with the information provided: you
haven''t revealed what INT16_T and INT32_T are.

If INT16_T is `short'' and INT32_T is `int'', the
two array elements are promoted to `int'', multiplied
to produce a 32-bit product, and added to `acc''.

If INT16_T is `int'' and INT32_T is `long'', the
two array elements are not promoted, the multiplication
produces a 16-bit product, and finally the product is
promoted to `long'' and added to `acc''.

If ... ah, pfui. I''m tired of running through all
the possible combinations; look ''em up yourself.

--
Er*********@sun.com


Eric Sosman <er*********@sun.com> writes:

Randy Yates wrote:

Consider the following code:

#include "dsptypes.h"



Nice of you to hide the crucial details where
nobody can see them ...

/* definitions */

#define VECTOR_LENGTH 64

/* local variables */

/* local function prototypes */

/* function definitions */

int main(int margc, char **margv)
{
UINT16_T n;
INT16_T x[VECTOR_LENGTH];
INT16_T y[VECTOR_LENGTH];
INT32_T acc;
INT16_T result;

acc = 0;
for (n = 0; n < VECTOR_LENGTH; n++)
{
x[n] = n;
y[n] = VECTOR_LENGTH - n - 1;
}

acc = 0;
for (n = 0; n < VECTOR_LENGTH; n++)
{
acc += x[n] * y[n];
}

result = (INT16_T)(acc >> 16);

return result;
}

What does ISO C say about how the conversions in the

acc += x[n] * y[n];

line are to be done? Specifically, is the multiply to be done with 16
bits, then the result promoted to 32 bits, or are x and y first to be
promoted to 32 bits before the multiply? Does the standard specify?



Unanswerable with the information provided: you
haven''t revealed what INT16_T and INT32_T are.

If INT16_T is `short'' and INT32_T is `int'', the
two array elements are promoted to `int'', multiplied
to produce a 32-bit product, and added to `acc''.

If INT16_T is `int'' and INT32_T is `long'', the
two array elements are not promoted, the multiplication
produces a 16-bit product, and finally the product is
promoted to `long'' and added to `acc''.

If ... ah, pfui. I''m tired of running through all
the possible combinations; look ''em up yourself.



Gladly, but I don''t have the spec, and I understand it''s
not cheap.

Here are the relevent typedefs:

typedef short INT16_T;
typedef long INT32_T;
--
Randy Yates
Sony Ericsson Mobile Communications
Research Triangle Park, NC, USA
ra*********@sonyericsson.com, 919-472-1124




Randy Yates wrote:

Eric Sosman <er*********@sun.com> writes:

If ... ah, pfui. I''m tired of running through all
the possible combinations; look ''em up yourself.
Gladly, but I don''t have the spec, and I understand it''s
not cheap.



Eighteen dollars, U.S. A textbook will probably cost
more, but it seems that might be a better investment for
you. No offense meant: Everybody starts from zero.
Here are the relevent typedefs:

typedef short INT16_T;
typedef long INT32_T;



long += short * short
-> long += (int)short * (int)short
-> long += int
-> long += (long)int
-> long += long

--
Er*********@sun.com


这篇关于类型转换问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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