分层寻呼地址掩码

时间:2018-03-17 17:00:01

标签: c bit-manipulation paging bitmask

我遇到了以下c代码,该代码旨在在 32位计算机上运行,并为2-hierarchical-中的page-directory-addresses和page-table-addresses生成位掩码分页:

#define PAGE_DIRECTORY_ADDRESS_MASK (~((uint64_t)0xfff))
#define PAGE_TABLE_ADDRESS_MASK (~((uint32_t)0xfff))

两者都不会返回相同的结果吗?

   20 bits       12 bits
+--------------+--------+
| 1...........1|0......0|
+--------------+--------+
32           12 11     0

1 个答案:

答案 0 :(得分:3)

好问题!它们似乎是相同的但是:

#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>

#define PAGE_DIRECTORY_ADDRESS_MASK (~((uint64_t)0xfff))
#define PAGE_TABLE_ADDRESS_MASK (~((uint32_t)0xfff))

int main()
{  
    uint64_t a = PAGE_DIRECTORY_ADDRESS_MASK;
    uint64_t b = PAGE_TABLE_ADDRESS_MASK;

    if (PAGE_DIRECTORY_ADDRESS_MASK != PAGE_TABLE_ADDRESS_MASK)
        printf("Different: %llx %llx\n",PAGE_DIRECTORY_ADDRESS_MASK, PAGE_TABLE_ADDRESS_MASK);

    if (a != b)
        printf("Different! %llx %llx\n",a, b);

    return 0;        
}

输出:

Different: fffffffffffff000 fffff000                                                                                                         
Different! fffffffffffff000 fffff000