Assuming pagesize
is the size of a page, they use bit masks to calculate the pointer (ptr
) to the start of the page containing func
, and the offset (offset
) within that page in bytes.
As pagesize
will always be a power of two, (pagesize-1)
has all ones set. The offset within the page is the last 12 (for instance) bits (12 corresponds with pagesize is 4k=2^12), so the first statement clears all the bits except the last 12 by using &
with an all ones bitmask of 12 least significant bits.
The second line calculates the pointer to the page itself by clearing the last 4 bits; by using the logical inverse (~
) of the previous bitmask, then an &
, it clears all the other bits.