What does "& 0xFFFFFFFF" do in this HIDWORD macro

I need a HIDWORD macro for the program I am creating and found it here: http://gnuwin32.sourceforge.net/compile.html

What bothers me, why is there &0xFFFFFFFF at the end?

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

How does this modify the output of this macro in any way?

+4
source share
3 answers

I do not think this has any real effect: (DWORD) final operation of the expression will force the result to 32 bits anyway.

The (DWORDLONG) causes the shift operation to act on an unsigned value, so no "sign bits" will be shifted to the intermediate result. However, since the operand can be 64-bit, there can still be non-zero bits at higher points than bit 31. The & 0xFFFFFFFF operation will reset these bits, but will also be executed (DWORD) .

But that won't hurt either. It can be argued that he makes the goal of macro-cleaning (except for you, maybe he's just joking!).

+4
source

It deals with a possible expansion of the character , clearly masking the high bits. Regardless of whether your compiler really decrypts the extension to correctly shift the negative value, the implementation is determined.

EDIT: Sign extension means setting high order bits to preserve the sign when shifting the number.

For instance:

 11111110 

is -2 if we assume that it is an 8-bit binary number. If we perform a simple logical shift , we get:

 01111111 

However, this changes the sign of the number. Many compilers will make an arithmetic shift to give:

 11111111 

Note that we fill in the most significant bit (this would be more than one for a more complex example) with 1.

+1
source

Assuming they are unsigned types and it is assumed that DWORD is 32 bits and DWORDLONG is 64 bits, then technically it does nothing.

Perhaps this is just the code left over from copying and pasting a similar macro for signed types?

Or perhaps one of the assumptions that I noticed is not fulfilled.

Cheers and hth.,

0
source

Source: https://habr.com/ru/post/1332182/


All Articles