常见问题 - 为什么演员? [英] FAQ Related - why cast?

查看:44
本文介绍了常见问题 - 为什么演员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与FAQ回答12.42有关的两个问题。


(1)声明


s.i16 | =(无符号)( getc(fp)<< 8);


i16被声明为int。转换为(未签名)的原因解释为

防止符号扩展。但是左移将始终填充腾出的空位(假设右边的操作数是非负的并且小于左边表达式中类型的位数的b / b) 。那么演员是如何有用的?


(2)我在下面的陈述中对未签名的演员感到困惑:


putc(( unsigned)((s.i32>> 24)& 0xff),fp);


i32被声明为long int。


据我所知,通常的算术转换将确保表达式的类型(s.i32>> 24)& 0xff将是long int。那个long int将是
转换为unsigned int,但重点是什么? putc()期望它的第一个

参数是int类型。所以目前正在经历


long int - > unsigned int - > int


如果没有演员表,那将是


long int - > int


---

马丁

Two questions relating to FAQ answer 12.42.

(1) In the statement

s.i16 |= (unsigned)(getc(fp) << 8);

i16 is declared int. The reason for casting to (unsigned) is explained as
guarding against sign extension. But left-shifting will always fill vacated
bits with zero (assuming the right operand is nonnegative and less than the
number of bits in the left expression''s type). So how is the cast useful?

(2) I am puzzled by the cast to unsigned in the following statement:

putc((unsigned)((s.i32 >> 24) & 0xff), fp);

i32 is declared long int.

As I understand it the usual arithmetic conversions will ensure the type of
the expression (s.i32 >> 24) & 0xff will be long int. That long int will be
cast to unsigned int, but what is the point? putc() expects its first
argument to be of type int. So at the moment it''s going through

long int -> unsigned int -> int

whereas without the cast it would be

long int -> int

---
Martin

推荐答案

在星期二,2005年1月4日16:39:52 -0000,马丁

< martin.o_brien @ [no-spam] which.net>在comp.lang.c中写道:
On Tue, 4 Jan 2005 16:39:52 -0000, "Martin"
<martin.o_brien@[no-spam]which.net> wrote in comp.lang.c:
与FAQ回答12.42有关的两个问题。


您必须拥有FAQ的书籍版本,因为12.42不在

在线版本中。

(1)在声明中

s.i16 | =(无符号)(getc(fp)<< 8);

i16声明为int。转换为(无符号)的原因解释为
防止符号扩展。但是左移总是用零填充空出的位(假设右操作数是非负的并且小于左表达式的类型中的位数)。那么演员如何有用?


如果getc()返回的int为负数,则左移它会产生

未定义的行为。如果getc()返回的int的值大于255,则左移它会产生未定义的行为。转换

将这些超出范围的值中的任何一个转换为unsigned int避免了

未定义的行为。

(2)我很困惑在以下声明中转为无符号:

putc((unsigned)((s.i32>> 24)& 0xff),fp);

i32是声明long int。

据我所知,通常的算术转换将确保表达式的类型(s.i32>> 24)& 0xff将是long int。那个long int将被转换为unsigned int,但有什么意义呢? putc()期望它的第一个
参数是int类型。所以目前正在经历

long int - > unsigned int - > int

如果没有演员阵容,那将是

long int - > int
Two questions relating to FAQ answer 12.42.
You must have the book version of the FAQ, since 12.42 is not in the
online version.
(1) In the statement

s.i16 |= (unsigned)(getc(fp) << 8);

i16 is declared int. The reason for casting to (unsigned) is explained as
guarding against sign extension. But left-shifting will always fill vacated
bits with zero (assuming the right operand is nonnegative and less than the
number of bits in the left expression''s type). So how is the cast useful?
If the int returned by getc() is negative, left shifting it produces
undefined behavior. If the int returned by getc() has a value greater
than 255, left shifting it produces undefined behavior. Converting
either of these out-of-range values to unsigned int avoids the
undefined behavior.
(2) I am puzzled by the cast to unsigned in the following statement:

putc((unsigned)((s.i32 >> 24) & 0xff), fp);

i32 is declared long int.

As I understand it the usual arithmetic conversions will ensure the type of
the expression (s.i32 >> 24) & 0xff will be long int. That long int will be
cast to unsigned int, but what is the point? putc() expects its first
argument to be of type int. So at the moment it''s going through

long int -> unsigned int -> int

whereas without the cast it would be

long int -> int




这有点草率编码。通常,位移不应该是有符号整数类型上使用的
。有太多潜在的惊喜

(读取缺陷,当程序没有做程序员预期的b $ b b)时。如果s.i32为负数,则移位的结果为

实现定义。实际上更有意义的是在转换前将

s.i32转换为无符号。


-

Jack Klein

主页: http://JK-Technology.Com

常见问题解答

comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html

comp.lang.c ++ http://www.parashift.com/c++-faq-lite/

alt.comp.lang.learn.c-c ++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html



This is somewhat sloppy coding. Generally, bit shifts should not be
used on signed integer types. There are too many potential surprises
(read defects, when the program does not do what the programmer
expected). If s.i32 is negative, the result of the shift is
implementation defined. It would actually make more sense to cast
s.i32 to unsigned long before the shift.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html


Jack Klein写道:
Jack Klein wrote:
s.i16 | =(unsigned)(getc(fp)<< 8);
s.i16 |= (unsigned)(getc(fp) << 8);



如果getc()返回的int为负数,左移它产生未定义的行为。如果getc()返回的int的值大于255,则左移会产生未定义的行为。



If the int returned by getc() is negative, left shifting it produces
undefined behavior. If the int returned by getc() has a value greater
than 255, left shifting it produces undefined behavior.




转换在转换之前完成,虽然。为了避免未定义的行为

你想要这样做:


s.i16 | =((unsigned)getc(fp))<< 8;


另外,getc不可能返回超过255的值,因为它

总是返回一个unsigned char值(sizeof(unsigned char)是

1)或负值(EOF为负)。

-

Derrick Coetzee

我授予此权限新闻组发布到公共领域。我不承担所有

明示或暗示保证并承担所有责任。我不是专业人士。



The shift is done before the cast, though. To avoid undefined behaviour
you would want to do:

s.i16 |= ((unsigned)getc(fp)) << 8;

Also, getc cannot possibly return a value exceeding 255, because it
always returns either an unsigned char value (sizeof(unsigned char) is
1) or a negative value (EOF is negative).
--
Derrick Coetzee
I grant this newsgroup posting into the public domain. I disclaim all
express or implied warranty and all liability. I am not a professional.


Derrick Coetzee写道:
Derrick Coetzee wrote:
Jack Klein写道:
Jack Klein wrote:
s.i16 | =(unsigned)(getc(fp)<< 8);
s.i16 |= (unsigned)(getc(fp) << 8);



如果getc返回的int( )是否定的,左移它产生未定义的行为。如果getc()返回的int的值大于255,则左移它会产生未定义的行为。


If the int returned by getc() is negative, left shifting it produces
undefined behavior. If the int returned by getc() has a value greater
than 255, left shifting it produces undefined behavior.



但是转换是在转换之前完成的。为了避免未定义的行为,你可能会这样做:

s.i16 | =((unsigned)getc(fp))<< 8;

另外,getc不可能返回超过255的值,因为它总是返回一个unsigned char值(sizeof(unsigned char)是
1)或者是否定的价值(EOF是负数)。


The shift is done before the cast, though. To avoid undefined behaviour
you would want to do:

s.i16 |= ((unsigned)getc(fp)) << 8;

Also, getc cannot possibly return a value exceeding 255, because it
always returns either an unsigned char value (sizeof(unsigned char) is
1) or a negative value (EOF is negative).




我不知道为什么杰克认为价值超过255

不能左移,但要确保getc()在一个字节上输出超过8位的系统

,这可能是完全的。这里的人有这样的实施工作。



I''m not sure why Jack thought that a value of greater than 255
could not be left-shifted, but be assured that it is entirely
possible for getc() to return a value exceding 255, on systems
with more than 8 bits to a byte. There are people here who have
worked on such implementations.


这篇关于常见问题 - 为什么演员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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