简体   繁体   English

这行代码是什么意思(按位运算符)

[英]What does this line of code mean (Bitwise Operator)

#define LODWORD(x) (*((unsigned int*)&(x)))

I'm translating C code to python and can't quite get this. 我正在将C代码转换为python,无法完全理解。 If anyone can explain how to read this or what it means it'd be greatly appreciated. 如果有人能解释如何阅读本手册或它的含义,将不胜感激。

&(x)                  // get address of `x` as a pointer to whatever x is
(unsigned int*)(...)  // cast it to a pointer to `unsigned int`
*(...)                // then read that address' contents as if it was `unsigned int`

I'd use union if really needed, and only if I knew the CPU architecture, otherwise this is very very unsafe :P 我会用union ,如果确实需要,且仅当我知道CPU架构,否则这是非常非常不安全的:P

It's a macro for getting the lower DWORD ( 32bits ) of a 64 bit variable, there's most likely an associated HIDWORD macro as well to get the higher 32 bits. 这是一个宏,用于获取64位变量的低DWORD (32位),最有可能是关联的HIDWORD宏也可以获取高32位。 Other comments have pointed out some flaws with the macro, but it's a fairly common idiom for accomplishing this. 其他评论指出了宏的一些缺陷,但这是完成此操作的相当普遍的习惯。

Some equivalent Python code might be: 一些等效的Python代码可能是:

def LODWORD(x):
    return x & 0xFFFFFFFF

First by using #define defines a macro that does substitution. 首先通过使用#define定义一个进行替换的宏。 It is a macro with an argument, a so-called "function-like macro". 它是带有参数的宏,即所谓的“函数式宏”。 After the #define when an expression LODWORD(whatever you write here) occurs it will be replaced by (*((unsigned int*)&(whatever you write here))) before the code is fed into the compiler. #define之后,当出现表达式LODWORD(whatever you write here)时,它将被(*((unsigned int*)&(whatever you write here)))替换,然后将代码送入编译器。 This is called "macro expansion". 这称为“宏扩展”。 The compiler will only see expanded expressions. 编译器将仅看到扩展表达式。

The macro expanded expression of LODWORD(foo) does the following: LODWORD(foo)的宏扩展表达式执行以下操作:

(foo) is common idiom in macros: Put arguments into parentheses to avoid operator precedence errors. (foo)是宏中的常见用法:将参数放在括号中,以避免运算符优先级错误。

&(foo) means "the address of (foo) " (a "pointer"). &(foo)的意思是“的地址(foo) ”(一个“指针”)。 This creates a value that represents the memory location of foo . 这将创建一个表示foo的内存位置的值。 It is of type "pointer to the type of foo " . 它的类型为“指向foo类型的指针”。

(unsigned int*)&(foo) converts "the adress of foo " into "the address of the unsigned int foo ". (unsigned int*)&(foo)将“ foo的地址”转换为“ unsigned int foo的地址”。 The operator (insigned int*) is called a "cast operator". 运算符(insigned int*)称为“ (insigned int*)运算符”。 It changes the result type "pointer to the type of foo " into "pointer to an unsigned int ". 它将结果类型“ pointer to foo of type”更改为“ pointer to unsigned int ”。

((unsigned int*)&(foo)) overrides operator precedence. ((unsigned int*)&(foo))覆盖运算符优先级。 Now you have "pointer to an unsigned int at the memory location of foo . 现在,您可以“指向foo的内存位置中的unsigned int

*((unsigned int*)&(foo)) returns the value of the unsigned int at the memory location of foo (even if foo is not an unsigned integer and even if that memory location violates alignment requirements for an unsigned int ). *((unsigned int*)&(foo))返回的值unsigned int在的存储位置foo (即使foo不是一个无符号的整数,并且即使该存储器位置违反了一个对齐的要求unsigned int )。

(*((unsigned int*)&(foo))) is yet another common idiom in macros: Put the entire expression into parentheses to avoid operator precedence errors. (*((unsigned int*)&(foo)))是宏中的另一个常见用法:将整个表达式放在括号中以避免运算符优先级错误。 Then the macro can always be used as if it was a function. 这样,宏就可以始终像是一个函数一样使用。

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

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