整数推广? [英] Integer Promotion?

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

问题描述

我着手试图找到一种可移植的方式来设置UCHAR_MAX的值。首先是

,我认为以下内容可行:


#define UCHAR_MAX~((unsigned char)0)

但是,它对我不起作用。有人可以向我解释一下'

是怎么回事?我原以为会发生以下情况:


(1)文字0,其类型为int,转换为unsigned char。


0000 0000

(2)结果无符号字符的所有位都被翻转。


1111 1111

我的预感是工作中有某种整数提升,但是我不知道它是怎么回事。


有人可以赐教我吗?

-


Frederick Gotham

解决方案

< blockquote>

Frederick Gotham写道:

我着手试图找到一种可移植的方式来设置UCHAR_MAX的值。首先,我认为以下内容可行:

#define UCHAR_MAX~((unsigned char)0)




什么限制版本中的版本错了吗?


Frederick Gotham< fg ******* @ SPAM.com>写道:

我着手试图找到一种可移植的方式来设置UCHAR_MAX的值。首先,我认为以下内容可行:

#define UCHAR_MAX~((unsigned char)0)

然而,它并没有起作用我。有人可以向我解释发生了什么吗?




〜的操作数受整数促销的影响,其中

表示在补语发生之前,`unsigned char''的值0被转换为int(或者,

,可能是unsigned int)。


具有正确值的表达式是(unsigned char)-1,但是

'不适合UCHAR_MAX,因为它包含一个演员和

,因为它有输入unsigned char,而UCHAR_MAX应该

的类型为int(或者,可能是unsigned int)。

-

我不知道没有那个方便的C& V,但我有Dan Pop。

--E。 Gibbons


----- BEGIN PGP签名消息-----

哈希:SHA1


Frederick Gotham写道:

我着手试图找到一种可移植的方式来设置UCHAR_MAX的值。首先,我认为以下内容可行:

#define UCHAR_MAX~((unsigned char)0)

然而,它并没有起作用我。




首先,你为什么试图/设置/给你一个(或应该是)

的价值你的C实现?你只是试图猜测你的编译器,或者你是否试图改变编译器所施加的限制b / b
。 FWIW,表示常量是(或应该)由编译器定义的
,仅用于您的信息外部化。


其次,你是什么意思它对我不起作用。你得到了什么?

你期望得到什么? #define怎么会让你失望?


[snip]

- -


Lew Pitcher,IT专家,企业技术解决方案,

企业技术解决方案,道明银行金融集团


(此处表达的意见是我自己的,而不是我的雇主的)< br $>
----- BEGIN PGP SIGNATURE -----

版本:GnuPG v1.4.3(MingW32)

评论:使用GnuPG Mozilla - http://enigmail.mozdev.org


iD8DBQFEicyxagVFX4UWr64RAhMXAJ9zUQiKCbqNtfmepCYpys BlVeML5ACfUsVc

/ llIMf24SnZYOd3pDY2vBM8 =

= 6eGA

----- END PGP SIGNATURE -----


I set about trying to find a portable way to set the value of UCHAR_MAX. At
first, I thought the following would work:

#define UCHAR_MAX ~( (unsigned char)0 )
However, it didn''t work for me. Could someone please explain to me what''s
going on? I would have thought that the following happens:

(1) The literal, 0, whose type is int, gets converted to an unsigned char.

0000 0000

(2) The resultant unsigned char then has all its bits flipped.

1111 1111
My hunch is that there''s some sort of integer promotion at work, but I
don''t know exactly how it works.

Could someone please enlighten me?
--

Frederick Gotham

解决方案


Frederick Gotham wrote:

I set about trying to find a portable way to set the value of UCHAR_MAX. At
first, I thought the following would work:

#define UCHAR_MAX ~( (unsigned char)0 )



What''s wrong with the version in limits.h?


Frederick Gotham <fg*******@SPAM.com> writes:

I set about trying to find a portable way to set the value of UCHAR_MAX. At
first, I thought the following would work:

#define UCHAR_MAX ~( (unsigned char)0 )
However, it didn''t work for me. Could someone please explain to me what''s
going on?



The operand of ~ is subject to the integer promotions, which
means that the `unsigned char'' value 0 is converted to int (or,
possibly, to unsigned int) before the complement happens.

An expression with the right value is (unsigned char) -1, but
that''s not suitable for UCHAR_MAX because it contains a cast and
because it has the type unsigned char, whereas UCHAR_MAX should
have type int (or, possibly, unsigned int).
--
"I don''t have C&V for that handy, but I''ve got Dan Pop."
--E. Gibbons


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Frederick Gotham wrote:

I set about trying to find a portable way to set the value of UCHAR_MAX. At
first, I thought the following would work:

#define UCHAR_MAX ~( (unsigned char)0 )
However, it didn''t work for me.



First off, why are you trying to /set/ a value that is (or should be)
supplied to you by your C implementation? Are you just trying to
second-guess your compiler, or are you trying to change the limit that
the compiler imposes. FWIW, that manifest constant is (or should be)
defined by the compiler, and externalized for your information only.

Secondly, what do you mean by "it didn''t work for me". What did you get?
What did you expect to get? How did the #define fail you?

[snip]
- --

Lew Pitcher, IT Specialist, Corporate Technology Solutions,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer''s)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFEicyxagVFX4UWr64RAhMXAJ9zUQiKCbqNtfmepCYpys BlVeML5ACfUsVc
/llIMf24SnZYOd3pDY2vBM8=
=6eGA
-----END PGP SIGNATURE-----


这篇关于整数推广?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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