C语言 #define FOO 1u 2u 4u ... 1u 和 2u 是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14844119/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
#define FOO 1u 2u 4u ... What does 1u and 2u mean?
提问by user2066639
I'm working with the HCS12 MCU, and this was part of the library. I'm just wondering what the 1U, 2U, 4U, 8U... means in this code. I'm still learning how to use classes, please try to explain things in layman terms.
我正在使用 HCS12 MCU,这是库的一部分。我只是想知道 1U、2U、4U、8U……在这段代码中的含义。我还在学习如何使用类,请尽量通俗易懂地解释一下。
My guess: Does this just mean which bit is set as high? 1U = 00000001 2U = 00000010 ...
我的猜测:这是否只是意味着哪个位设置为高?1U = 00000001 2U = 00000010 ...
What would this be called?
这会叫什么?
Thanks
谢谢
typedef union {
byte Byte;
struct {
byte :1;
byte ADR1 :1; /* Slave Address Bit 1 */
byte ADR2 :1; /* Slave Address Bit 2 */
byte ADR3 :1; /* Slave Address Bit 3 */
byte ADR4 :1; /* Slave Address Bit 4 */
byte ADR5 :1; /* Slave Address Bit 5 */
byte ADR6 :1; /* Slave Address Bit 6 */
byte ADR7 :1; /* Slave Address Bit 7 */
} Bits;
struct {
byte :1;
byte grpADR_1 :7;
} MergedBits;
} IBADSTR;
extern volatile IBADSTR _IBAD @(REG_BASE + 0x000000E0UL);
#define IBAD _IBAD.Byte
#define IBAD_ADR1 _IBAD.Bits.ADR1
#define IBAD_ADR2 _IBAD.Bits.ADR2
#define IBAD_ADR3 _IBAD.Bits.ADR3
#define IBAD_ADR4 _IBAD.Bits.ADR4
#define IBAD_ADR5 _IBAD.Bits.ADR5
#define IBAD_ADR6 _IBAD.Bits.ADR6
#define IBAD_ADR7 _IBAD.Bits.ADR7
#define IBAD_ADR_1 _IBAD.MergedBits.grpADR_1
#define IBAD_ADR IBAD_ADR_1
#define IBAD_ADR1_MASK 2U
#define IBAD_ADR2_MASK 4U
#define IBAD_ADR3_MASK 8U
#define IBAD_ADR4_MASK 16U
#define IBAD_ADR5_MASK 32U
#define IBAD_ADR6_MASK 64U
#define IBAD_ADR7_MASK 128U
#define IBAD_ADR_1_MASK 254U
#define IBAD_ADR_1_BITNUM 1U
回答by user7116
It defines an unsigned integer literal. You can also see where they defined a hex literal to be an unsigned long integerby using 0x...UL.
它定义了一个无符号整数文字。您还可以unsigned long integer通过使用0x...UL.
If you would like to know the bit pattern they produce, simply translate the decimal literals to their equivalent hex or binary literals. 1Ubecomes 0x01Uand 01b1in hex and binary respectively.
如果您想知道它们产生的位模式,只需将十进制文字转换为其等效的十六进制或二进制文字。分别在十六进制和二进制中1U变为0x01U和01b1。
Another more commonly seen literal uses the f-suffix, that is a single precision floating point literal like 1.0f.
另一个更常见的文字使用 -f后缀,即单精度浮点文字,如1.0f.
1. for illustration only, not an actual literal per the standard
1. 仅作说明,并非符合标准的实际文字
回答by Samuel Edwin Ward
They're just integer constants. The Usuffix makes them unsigned ints instead of ints.
它们只是整数常量。该U后缀使他们无符号整数,而不是整数。
This is described in section 6.4.4.1 of the final draft of the C11 standard.
这在 C11 标准最终草案的第 6.4.4.1 节中有描述。
回答by Nisarg
In Embedded systems U literal means that the value is unsigned integer
在嵌入式系统中,U 字面量表示该值是无符号整数
you can find some usefull content here.
你可以在这里找到一些有用的内容。

