小编Man*_*rez的帖子

如何从确定的位置检查一组位是否为0?

我正在写一个位图物理内存管理器,我希望实现一个函数,检查一个位是否从特定位开始是空闲的.现在我使用这个函数来检查单个位是否空闲,我调用它n次以查看n位是否空闲,但我认为这样做效率不高:

inline static bool physical_memory_map_test(uint32_t bit)
{
    return physical_memory.blocks[bit/32] & (1 << bit % 32);
}
Run Code Online (Sandbox Code Playgroud)

所以我想实现这样的东西:("包含伪代码):

static bool physical_memory_map_test(uint32_t starting_bit, uint32_t count)
{
    int excess = (starting_bit%32 + count) -32;
    if(excess < 0)
        return (physical_memory.blocks[bit/32] & "-excess number of 1s" << bit % 32)) && (physical_memory.blocks[bit/32] & "count + excess number of 1s" << bit % 32));

    return physical_memory.blocks[bit/32] & ("count number of ones, if count is 3, this should be 111" << bit % 32); 
}
Run Code Online (Sandbox Code Playgroud)

或者更好的方法来检查所有位是否为0(返回true)或者其中至少有一位是1(返回false)我怎么能这样做?

c bit-manipulation bitmapdata

0
推荐指数
1
解决办法
124
查看次数

标签 统计

bit-manipulation ×1

bitmapdata ×1

c ×1