zoukankan      html  css  js  c++  java
  • 常见的位运算技巧总结(膜wys)

    看了wys的论文,感觉获得了不少新姿势

    这里总结一下

    #include <iostream>
    using namespace std;
    typedef unsigned int u32;
    
    inline u32 read_bit(u32 x, int pos){
        return (x >> pos)&1;
    }
    inline u32 set_bit(u32 x, int pos){
        return x | (1u << pos);    
    }
    inline u32 clear_bit(u32 x, int pos){
        return x & ~(1u << pos);
    }
    
    int cnt_table[1 << 16];
    void count_pre(){
        cnt_table[0] = 0;
        for(int i = 0; i < 1<<16; i++){
            cnt_table[i] = cnt_table[i >> 1] + (i & 1);
        }
    }
    inline int count(u32 x){
        return cnt_table[x >> 16] + cnt_table[x & 65535u];
    }
    
    inline int count_trailing_zeros(u32 x){
        int ret = 0;
        if(!(x & 65535u)) x >>= 16, ret |= 16;
        if(!(x & 255u)) x >>= 8, ret |= 8;
        if(!(x & 15u)) x >>= 4, ret |= 4;
        if(!(x & 3u)) x >>= 2, ret |= 2;
        if(!(x & 1u)) x >>= 1, ret |= 1;
        return ret + !x;
    }
    
    int clz_table[1 << 16];
    void clz_pre(){
        clz_table[0] = 16;
        for(int i = 1; i < 1 << 16; i++){
            clz_table[i] = clz_table[i >> 1] - 1;
        }
    }
    
    inline int count_learding_zero(u32 x){
        return x >> 16 ? clz_table[x >> 16] : 16 + clz_table[x & 65535u];
    }
    
    inline u32 lowbit(u32 x){
         return x & -x;
    }
    
    //ö¾Ù×Ó¼¯
    void subS(int S){
        for(int i = S; i; i = (i-1)&S){
            //do_something(i);
        }
    }
    
    int main() { return 0; }
  • 相关阅读:
    HashMap Hashtable LinkedHashMap 和TreeMap
    RestTemplate -springwebclient
    IntelliJ IDEA 12:
    mac安装RabbitMQ
    mysql 常用,使用经验
    消息中间件性能究竟哪家强?
    log4j2配置文件log4j2.xml
    内存增长 避免
    nginx 服务器重启命令,关闭
    jquery获取css color 值返回RGB
  • 原文地址:https://www.cnblogs.com/Saurus/p/6536361.html
Copyright © 2011-2022 走看看