(0x41u * 0x201)& 0x7FFF未定义? [英] (0x41u*0x201)&0x7FFF undefined?

查看:130
本文介绍了(0x41u * 0x201)& 0x7FFF未定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


表达式

(0x41u * 0x201)& 0x7FFF

产生明确定义的值0x0241或者是未定义的行为?


换句话说:什么是未签名的产品类型?
int,并且使用常数进行更改?


感谢您帮助我完全理解表达式的类型。


Fran?ois Grieu

Hello,

does the expression
(0x41u*0x201)&0x7FFF
yield the well defined value 0x0241, or is the behaviour undefined?

In other words: what''s the type of the product on an unsigned by an
int, and does the use of constants make a change ?

Thanks for helping me fully understand typedness of expressions.

Fran?ois Grieu

推荐答案



" Francois Grieu" < fg **** @ gmail.com写在

"Francois Grieu" <fg****@gmail.comwrote in

>表达式
(0x41u * 0x201)& 0x7FFF
产量定义好的值0x0241,还是行为未定义?
>does the expression
(0x41u*0x201)&0x7FFF
yield the well defined value 0x0241, or is the behaviour undefined?


>换句话说:什么是无符号的产品类型?
int,是否使用常数会改变吗?
>In other words: what''s the type of the product on an unsigned by an
int, and does the use of constants make a change ?


>感谢您帮助我完全理解表达式的类型。
>Thanks for helping me fully understand typedness of expressions.



裸整数的类型为int。

通常一个int至少有32位,所以代码很好。

如果int应该是16位,则会出现溢出。如果int已签名,则这是
未定义的行为。在实践中,任何非反常的编译器都会给你一个

编译时诊断,因为表达式是一个常量。准确地说,如果一个反常的编译器会如何行动,你可以看看

标准的点和标题。


但是你的整数是无符号的 - 默认情况下为十六进制。所以溢出是

很好的定义并且必须是一个环绕。

Bare integer are of type int.
Normally an int has at least 32 bits, so the code is fine.
If int should be 16 bits, you get an overflow. If the int is signed, this is
undefined behaviour. In practise any non-perverse compiler would give you a
compile time diagnostic because the expression is a constant. For exactly
how a perverse compiler would act you can look at the dots and tittles of
the standard.

However your integers are unsigned - hex is by default. So the overflow is
well defiend and has to be a wrapround.


在文章< DM ********* *********************@bt.com> ;,

" Malcolm McLean" < re ******* @ btinternet.comwrote:
In article <DM******************************@bt.com>,
"Malcolm McLean" <re*******@btinternet.comwrote:

" Francois Grieu" < fg **** @ gmail.comwrote
"Francois Grieu" <fg****@gmail.comwrote

>表达式
(0x41u * 0x201)& 0x7FFF
产生明确定义的值0x0241,还是未定义的行为?
>does the expression
(0x41u*0x201)&0x7FFF
yield the well defined value 0x0241, or is the behaviour undefined?


>换句话说:什么是无符号的产品类型?
int,是否使用常数会改变吗?
>In other words: what''s the type of the product on an unsigned by an
int, and does the use of constants make a change ?


>感谢您帮助我完全理解表达式的类型。
>Thanks for helping me fully understand typedness of expressions.


通常一个int至少有32位,所以代码没问题。
Normally an int has at least 32 bits, so the code is fine.



在此组中,正态性为MAXINT> = 32767

In this group, normality is MAXINT >=32767


如果int应为16位,你得到一个溢出。如果int已签名,则这是
未定义的行为。在实践中,任何非反常的编译器都会给你一个

编译时诊断,因为表达式是一个常量。准确地说,如果一个反常的编译器会如何行动,你可以看看

标准的点和标题。


但是你的整数是无符号的 - 默认情况下为十六进制。
If int should be 16 bits, you get an overflow. If the int is signed, this is
undefined behaviour. In practise any non-perverse compiler would give you a
compile time diagnostic because the expression is a constant. For exactly
how a perverse compiler would act you can look at the dots and tittles of
the standard.

However your integers are unsigned - hex is by default.



这对我来说似乎不对。我对标准的解读是0x201

是一个(签名的)int,而不是unsigned int。


6.4.4常量:的类型一个整数常量是chich中相应列表的第一个

,其值可以表示为


后缀十进制常量八进制或十进制常量

none int int

long int unsigned int

long long int long int

unsigned long int

long long int

unsigned long long int

u或U unsigned int unsigned int

unsigned long int unsigned long int

unsigned long long int unsigned long int

unsigned long long int

(..)

我希望我得到的类型常数是正确的,但我承认我总是

与表达式的类型混淆,可以安全地假设

关于ove的表达式rflow。

Fran?ois Grieu

That seems incorrect to me. My reading of the standard is that 0x201
is a (signed) int, not an unsigned int.

6.4.4 constant: "the type of an integer constant is the first
of the corresponding list in chich its value can be represented"

Suffix Decimal constant Octal or Hecadecimal constant
none int int
long int unsigned int
long long int long int
unsigned long int
long long int
unsigned long long int

u or U unsigned int unsigned int
unsigned long int unsigned long int
unsigned long long int unsigned long int
unsigned long long int
(..)
I hope I get the type of constants right, but I admit I am allways
confused with the type of expressions, and what can be safely assumed
about expressions that overflow.
Fran?ois Grieu


" Malcolm McLean" < re ******* @ btinternet.comwrites:
"Malcolm McLean" <re*******@btinternet.comwrites:

" Francois Grieu" < fg **** @ gmail.com写在
"Francois Grieu" <fg****@gmail.comwrote in

>>表达式
(0x41u * 0x201)& 0x7FFF 产生明确定义的值0x0241,还是行为未定义?
>>does the expression
(0x41u*0x201)&0x7FFF
yield the well defined value 0x0241, or is the behaviour undefined?


>>换句话说:什么是无符号的产品类型?
int使用常数会有所改变吗?
>>In other words: what''s the type of the product on an unsigned by an
int, and does the use of constants make a change ?


>>感谢您帮助我完全理解表达式的类型。
>>Thanks for helping me fully understand typedness of expressions.



裸整数的类型为int。


Bare integer are of type int.



如果通过裸你的意思是整数常量,没有0或0x前缀,没有

后缀(u / l / ul / lu / ll / ull / llu及其大写等价物)然后

他们也可以是long int或long long int类型,如果常量需要它的大小为


If by "bare" you mean integer constants with no 0 or 0x prefix and no
suffix (u/l/ul/lu/ll/ull/llu and their uppercase equivalents) then
they may also be of type long int or long long int if the magnitude of
the constant requires it.


但是你的整数是无符号的 - 十六进制是默认值。所以溢出是好的b $ b很好的定义,并且必须是一个环绕。
However your integers are unsigned - hex is by default. So the overflow is
well defiend and has to be a wrapround.



编号带有0或0x前缀的非后缀整数常量被签名

如果它适合有符号的int(或者成长的int或long long

int)。由前缀引起的唯一不同是它* *可能是* b $ b被视为unsigned int(或长或长版本)如果

它适合一个。


-

Ben。

No. With a 0 or 0x prefix an un-suffixed integer constant is signed
if it will fit into a signed int (or into a long int or long long
int). The only different caused by the prefix is that it *may* be
taken as being an unsigned int (or the long or long long versions) if
it fits in one.

--
Ben.


这篇关于(0x41u * 0x201)&amp; 0x7FFF未定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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