【编程珠矶】开篇 千万号码的排序问题

本文详细介绍了位图数据结构的基本概念及其操作方法,包括位图的设置、测试、清除等核心函数实现,并提供了具体的示例代码。此外还介绍了位运算符如位与、位或、位异或以及掩码的概念和应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

注:学习了《编程珠矶》第一章,将涉及的一些知识点整理如下

 

value-->index 将某个数转化为位图的索引,如要表示value=2,即将位图index=2上的value赋为1(00100000.。。)

 

位图数据结构

~位反

<< 位左移

>> 位右移

& 位与 : 同为1(真)为1

| 位或 :只要有一个为1(真)则为1

^ 位异或 :1 和 0为1

 

0,1 | 1 = 1,1    ==> 或1,都成1

0,1 | 0 = 0,1

0,1 & 1 = 0,1

0,1 & 0 = 0,0  ==> 与0,都成0

0,1 ^ 1 = 1,0  ==> 异或1,变反

0,1 ^ 0 = 0,1

掩码
掩码在计算机学科及数字逻辑中指的是一串二进制数字,通过与目标数字的按位操作,达到屏蔽指定位而实现需求。
示例:创造一个掩码msk把一个指令cmd的第0~3位(右边第一位为0位)清零:

指令 cmd = 0110011011
创造掩码 msk = 0000001111

用掩码的反码~msk和指令cmd做按位与运算 cmd & ~msk = 0110011011 & 1111110000 = 0110010000则指定的第0~3位已被清零。

 

    #define BITS_PER_BYTE 8  
    #define BITS_PER_INT (BITS_PER_BYTE * 4)  
    // set the bit of the int specified by index to 1.  
    // the index is zero-based, counting from the left(the higher end)  
    inline void pearl_int_set(int* data, int index)  
    {  
        int mask = 1 << (BITS_PER_INT - 1 - index);  
        *data |= mask;  
    }  
      
    // test whether a bit of the int specified by index is 1.  
    // the index is zero-based, counting from the left(the higher end)  
    inline int pearl_int_test(int* data, int index)  
    {  
        int mask = 1 << (BITS_PER_INT - 1 - index);  
        return (*data) & mask;  
    }  
      
    // set a bit of the int specified by bit_index to 0.  
    // the index is zero-based, counting from the left(the higher end)  
    inline void pearl_int_clear(int *data, int bit_index)  
    {  
        int mask = ~(1 << (BITS_PER_INT - 1 - bit_index));  
        *data &= mask;  
    }  
      
    // get the right(lower) part of the int.  
    inline int pearl_int_right(int *data, int count)  
    {  
        int mask = 1;  
        while(count > 0) {  
            mask = mask << 1 + 1;  
        }  
        return *data & mask;      
    }  
      
    // get the left(upper) part of the int value  
    inline int pearl_int_left(int *data, int count)  
    {  
        int mask = 1;  
        count = BITS_PER_BYTE - count;  
        while (count > 0) {  
            mask = mask << 1 + 1;  
        }  
      
        mask = ~mask;  
        return *data & mask;  
    }  
      
    // set a bit of the speicified memory area to 1.  
    // @warning this function does NOT perform error checking.  
    inline void pearl_set(int *bitplane, int index)  
    {  
        int offset = index / BITS_PER_INT;  
        int pos = index - offset * BITS_PER_INT;  
      
        pearl_int_set(bitplane + offset, pos);  
    }  
      
    // set a bit of the specified memory area to 0.  
    // @warning this function does NOT perform error checking.  
    inline void pearl_clear(int *bitplane, int index)  
    {  
        int offset = index / BITS_PER_INT;  
        int pos = index - offset * BITS_PER_INT;  
      
        pearl_int_clear(bitplane + offset, pos);  
    }  
      
    // test if a bit of the specified memory area is 1.  
    // @warning this function does NOT perform error checking.  
    inline int pearl_test(int *bitplane, int index)  
    {  
        int offset = index / BITS_PER_INT;  
        int pos = index - offset * BITS_PER_INT;  
      
        return pearl_int_test(bitplane + offset, pos);  
    }  
 

如果待排序数据都在内存中

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值