“1u”的含义 [英] meaning of "1u"

查看:488
本文介绍了“1u”的含义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这应该是一个简单的...1u的含义是什么,例如:


#define DUMMY(1U<< 31)

而不仅仅是:


#define DUMMY(1<< 31)

this should be an easy one ... what is the meaning of "1u", for instance:

#define DUMMY (1U<<31)

as opposed to just:

#define DUMMY (1<<31)

推荐答案

df****@earthlink.net (godfather2)写在

新闻:d1 ************************** @ posting.google.c om:
df****@earthlink.net (godfather2) wrote in
news:d1**************************@posting.google.c om:
这应该是一个简单的...1u的意思是什么,对于
实例:

#define DUMMY(1U<< 31)

而不仅仅是:

#define DUMMY(1<< 31)
this should be an easy one ... what is the meaning of "1u", for
instance:

#define DUMMY (1U<<31)

as opposed to just:

#define DUMMY (1<<31)




前者迫使C将31推广到unsigned int匹配unsigned

int 1U。这意味着生成的表达式将具有

unsigned int(或unsigned long取决于平台)的类型,而不是

签名的替代品,这可能不需要。


-

- 马克 - >

-



The former forces C to promote 31 to an unsigned int to match the unsigned
int 1U. This means that the resulting expression will have a type of
unsigned int (or unsigned long depending upon the platform) instead of the
signed alternative which one may not want.

--
- Mark ->
--


Mark A. Odell <无**** @ embeddedfw.com>写道:
"Mark A. Odell" <no****@embeddedfw.com> writes:
df****@earthlink.net (godfather2)写道
新闻:d1 ************************** @ posting.google.c om:
df****@earthlink.net (godfather2) wrote in
news:d1**************************@posting.google.c om:
这应该是一个简单的...1u的含义是什么,对于
实例:

#define DUMMY(1U<< 31)
而不仅仅是:

#define DUMMY(1<<< 31)
this should be an easy one ... what is the meaning of "1u", for
instance:

#define DUMMY (1U<<31)

as opposed to just:

#define DUMMY (1<<31)



前者迫使C将31推广为无符号int匹配unsigned
int 1U。这意味着生成的表达式将具有
unsigned int(或unsigned long取决于平台)的类型,而不是
签名的替代,这可能是您不想要的。



The former forces C to promote 31 to an unsigned int to match the unsigned
int 1U. This means that the resulting expression will have a type of
unsigned int (or unsigned long depending upon the platform) instead of the
signed alternative which one may not want.




不,表达式将具有unsigned int类型,无论平台是否为
。值1总是适合无符号。另一方面,如果平台的无符号整数小于32位

宽,则1U << 31未定义。

-

如果行为未定义,这将是一个更好的未定义行为示例



- -Michael Rubenstein



No, the expression will have a type of `unsigned int'' regardless
of the platform. The value 1 always fits in an unsigned. On the
other hand, if the platform''s unsigned ints are less than 32 bits
wide, then 1U<<31 is undefined.
--
"It would be a much better example of undefined behavior
if the behavior were undefined."
--Michael Rubenstein




" godfather2" < DF **** @ earthlink.net>在消息中写道

news:d1 ************************** @ posting.google.c om ...

"godfather2" <df****@earthlink.net> wrote in message
news:d1**************************@posting.google.c om...
这应该是一个简单的...1u的含义是什么,例如:

#define DUMMY(1U<< 31)

而不仅仅是:

#define DUMMY(1<< 31)
this should be an easy one ... what is the meaning of "1u", for instance:

#define DUMMY (1U<<31)

as opposed to just:

#define DUMMY (1<<31)




在32平台上-bit int',前者是unsigned int(0x80000000),

后者是int(-2147483648)。

相同位模式。


我读C99标准的方式,后者将导致未定义的

行为。

" E1的结果<< ; E2是E1左移E2位位置;腾出的位

充满了

零。如果E1具有无符号类型,则结果的值为E1 *(2 ** E2),

减少模数

比结果中可表示的最大值多一个类型。如果E1已经签署

类型和非负值,并且E1 *(2 ** E2)在结果中可以表示

类型,那么那是

结果值;否则,行为未定义。

Carsten



On a platform with 32-bit int''s, the former is an unsigned int (0x80000000),
the latter is an int (-2147483648).
Same bit pattern.

The way I read the C99 Standard, the latter will result in undefined
behavior.
"The result of E1 << E2 is E1 left-shifted E2 bit positions; vacated bits
are filled with
zeros. If E1 has an unsigned type, the value of the result is E1 * (2**E2),
reduced modulo
one more than the maximum value representable in the result type. If E1 has
a signed
type and nonnegative value, and E1 * (2**E2) is representable in the result
type, then that is
the resulting value; otherwise, the behavior is undefined."
Carsten


这篇关于“1u”的含义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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