简体   繁体   中英

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.

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.

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.

The first thing to note about this code is that none of it will actually be compiled into 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 . 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). 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 .


#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 ). Otherwise, use the GNU C datatype ( 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. This line ends the #ifdef THIRTYTWO_BIT declaration made previously.


#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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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