在C中这个宏定义了什么?

Joh*_*ica 1 c macros bit-shift c-preprocessor

#define NUM_SQ_0 (1 << 1*1)  //<<----- ??

static struct square sq_0[NUM_SQ_0];
Run Code Online (Sandbox Code Playgroud)

请注意,square是一个带有4个指向square的指针的结构,定义如下:

typedef struct square {
    struct square *nw, *ne,
                  *sw, *se;
} *square;
Run Code Online (Sandbox Code Playgroud)

unw*_*ind 7

它评估为左移1位,即数字也称为2.

由于乘法运算符*比位移运算符更紧密地绑定<<,因此表达式被解析为1 << (1 * 1),即只是1 << 1.

在二进制中,使用8位可读性,我们有

  00000001
<<       1        
==========
  00000010
Run Code Online (Sandbox Code Playgroud)

转换回十进制,我们得到00000010 2 = 2 10.