简体   繁体   English

谁能解释一下这段代码

[英]Can anyone explain me this code

#ifndef EIGHT_BIT
#define THIRTYTWO_BIT // default 32 bit
#endif

#ifdef THIRTYTWO_BIT
#define WORD unsigned long
#define WORDLENGTH 4

#if defined(WIN32) && !defined(__GNUC__)
#define WORD64  unsigned __int64
#else
#define WORD64  unsigned long long
#endif

// THIRTYTWO_BIT
#endif


#ifdef EIGHT_BIT

#define WORD unsigned short
#define WORDLENGTH 4

// EIGHT_BIT
#endif

It's just a definition of constants (aka defines) depending on the #define EIGHT_BIT.它只是常量的定义(又名定义),具体取决于#define EIGHT_BIT。

If EIGHT_BIT is defined, WORD means unsigned short and WORDLENGTH is 4. Otherwise, WORD is unsigned long and WORDLENGTH is also 4. Additionally, WORD64 will be defined as unsigned long long unless you are on a WIN32 system and not using GCC.如果定义了 EIGHT_BIT,WORD 表示 unsigned short,WORDLENGTH 为 4。否则,WORD 为 unsigned long,WORDLENGTH 也是 4。此外,WORD64 将被定义为 unsigned long long,除非您在 WIN32 系统上并且不使用 GCC。

All the "code" does it setup pre-processor symbols for any "live" code that you do have.所有“代码”都会为您拥有的任何“实时”代码设置预处理器符号。 If a symbol called EIGHT_BIT is defined before this code is pre-processed, it sets up WORD and WORDLENGTH accordingly (though WORDLENGTH 's value is suspect), and it will set up the values differently if EIGHT_BIT is not already defined.如果在预处理此代码之前定义了名为EIGHT_BIT的符号,它会相应地设置WORDWORDLENGTH (尽管WORDLENGTH的值是可疑的),如果尚未定义EIGHT_BIT ,它将以不同的方式设置值。

The first thing to note about this code is that none of it will actually be compiled into C.关于这段代码,首先要注意的是它实际上都不会被编译成 C。 Every line that isn't whitespace or a comment starts with a pound sign ( # ), meaning they are preprocessor directives.不是空格或注释的每一行都以井号 ( # ) 开头,这意味着它们是预处理器指令。 A preprocessor directive alters the code before it even makes it to the compiler.预处理器指令甚至在将代码提交给编译器之前就对其进行了更改。 For more information of preprocessor directives, see this article .有关预处理器指令的更多信息,请参阅这篇文章

Now that we know that much, let's look through the code:现在我们知道了很多,让我们看一下代码:


#ifndef EIGHT_BIT
#define THIRTYTWO_BIT // default 32 bit
#endif

If the macro EIGHT_BIT is not defined, define another macro called THIRTYTWO_BIT .如果未定义宏EIGHT_BIT ,则定义另一个名为THIRTYTWO_BIT的宏。 This is most likely referring to the number of bits in a word on a processor.这很可能是指处理器上一个字的位数。 This code intends to be cross-platform, meaning that it can run on a number of processors.此代码旨在跨平台,这意味着它可以在多个处理器上运行。 The snippet you posted pertains to managing different word widths.您发布的片段与管理不同的字宽有关。


#ifdef THIRTYTWO_BIT
#define WORD unsigned long
#define WORDLENGTH 4

If the macro THIRTYTWO_BIT is defined, then define a WORD to be an unsigned long , which has a WORDLENGTH of 4 (presumably bytes).如果定义了宏THIRTYTWO_BIT ,则将WORD定义为unsigned long ,它的WORDLENGTH为 4(可能是字节)。 Note that this statement isn't necessarily true, as the C standard only guarantees that a long will be as least as long as an int .请注意,此声明不一定正确,因为 C 标准仅保证long至少与int一样长。


#if defined(WIN32) && !defined(__GNUC__)
#define WORD64  unsigned __int64
#else
#define WORD64  unsigned long long
#endif

If this is a 32-bit Windows platform and the GNU C compiler is not available, then use the Microsoft-specific datatype for 64-bit words ( unsigned __int64 ).如果这是一个 32 位 Windows 平台,并且 GNU C 编译器不可用,则对 64 位字使用 Microsoft 特定的数据类型 ( unsigned __int64 )。 Otherwise, use the GNU C datatype ( unsigned long long ).否则,使用 GNU C 数据类型( unsigned long long )。


// THIRTYTWO_BIT
#endif

Every #if and #ifdef directive must be matched by a corresponding #endif to delineate where the conditional section ends.每个#if#ifdef指令都必须与相应的#endif匹配,以描述条件部分的结束位置。 This line ends the #ifdef THIRTYTWO_BIT declaration made previously.这一行结束了先前所做的#ifdef THIRTYTWO_BIT声明。


#ifdef EIGHT_BIT

#define WORD unsigned short
#define WORDLENGTH 4

// EIGHT_BIT
#endif

If the target processor has a word width of 8 bits, then define a WORD to be an unsigned short , and define the WORDLENGTH to be 4 (again, presumably in bytes).如果目标处理器的字宽为 8 位,则将WORD定义为unsigned short ,并将WORDLENGTH定义为 4(同样,大概以字节为单位)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM