简体   繁体   English

“&0xFFFFFFFF”在这个HIDWORD宏中做了什么

[英]What does the “&0xFFFFFFFF” do in this HIDWORD macro

I needed the HIDWORD macro for a program I'm making and found this one here: http://gnuwin32.sourceforge.net/compile.html 我需要HIDWORD宏来处理我正在编写的程序,并在此处找到了这个: http ://gnuwin32.sourceforge.net/compile.html

What I'm confused about is why there is a &0xFFFFFFFF at the end of it? 我感到困惑的是为什么在它的末尾有一个&0xFFFFFFFF

#define HIDWORD(l) ((DWORD)(((DWORDLONG)(l)>>32)&0xFFFFFFFF))

How does that modify the output of this macro in any way? 如何以任何方式修改此宏的输出?

I don't think it has any real effect - the (DWORD) cast that the final operation of the expression will force the result to 32 bits anyway. 我不认为它有任何实际效果 - (DWORD)强制转换表达式的最终操作将强制结果为32位。

The (DWORDLONG) cast forces the shift operation to act on an unsigned value, so no 'sign bits' will be shifted into the intermediate result. (DWORDLONG)强制转换强制移位操作作用于无符号值,因此不会将“符号位”移入中间结果。 However, since the operand could be 64-bits, there might still be non-zero bits at higher locations than bit 31. The & 0xFFFFFFFF operation will zero out those bits, but so would the (DWORD) cast. 但是,由于操作数可能是64位,因此在高于位31的位置可能仍然存在非零位。 & 0xFFFFFFFF操作将使这些位清零,但(DWORD)也是如此。

But it doesn't hurt, either. 但它也没有受到伤害。 One could argue that it makes the intent of the macro clearer (except to you, maybe - just kidding!). 有人可能会争辩说它会使宏的意图更清晰(除了你,也许 - 只是开玩笑!)。

It's dealing with possible sign extension by explicitly masking out the high-order bits. 它通过显式屏蔽高阶位来处理可能的符号扩展 Whether your compiler actually does sign extension for a right shift of a negative is implementation-defined. 您的编译器是否实际上负片的右移进行符号扩展是实现定义的。

EDIT: Sign extension refers to setting the high-order bits to keep the sign when a number is shifted. 编辑:符号扩展指的是设置高位以在数字移位时保持符号。

For example: 例如:

11111110

is -2, if we assume it's a 8-bit two's complement number. 如果我们假设它是一个8位二进制补码数,则为-2。 If we do a simple logical shift right, we get: 如果我们做一个简单的逻辑右移,我们得到:

01111111

However, that changes the sign of the number. 但是,这会改变数字的符号。 Many compilers will do a arithmetic shift, to give: 许多编译器会进行算术转换,给出:

11111111

Note that we fill in the most significant bit (it would be more than one for a more complex example) with 1's. 请注意,我们用1表示填写最重要的位(对于更复杂的示例,它将不止一个)。

Assuming those are unsigned types, and assuming DWORD is 32 bits and DWORDLONG is 64 bits, then technically it doesn't do anything. 假设这些是无符号类型,并假设DWORD是32位而DWORDLONG是64位,那么从技术上讲它不会做任何事情。

Perhaps it's just code left over from copy-and-paste of similar macro for signed types? 也许这只是代码类型的类似宏的复制和粘贴留下的代码?

Or perhaps one the assumptions I noted doesn't hold. 或者也许我注意到的一个假设并不成立。

Cheers & hth., 干杯&hth。,

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

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