您当前的位置: 首页 >  c++

Jave.Lin

暂无认证

  • 2浏览

    0关注

    704博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

C++ BitSet

Jave.Lin 发布时间:2020-05-15 12:50:07 ,浏览量:2

文章目录
  • 位操作符
  • bitset 操作
  • 自己编写一个BitSet
    • BitSet.h
    • b.h
    • a.cpp
    • 输出
  • References
还是温习C++

位操作符 操作符功能用法~按位非~expr> expr2&按位与expr1 & expr2^按位异或expr1 ^ expr2|按位或expr1 | expr2&=按位与赋值expr1 &= expr2^=按位异或赋值expr1 ^= expr2|=按位或赋值expr1 |=expr2
  • ~ 位反转:0转1,0转1
     00001000 // a
   ~ 00001000 // ~a
-------------
     11110111 // result = ~a
  • 1 ------------- 11110111 // result = a >> 1
    // 左侧补上0位的情况
         11101110 // a
      >> 11101110 // a >> 1
    -------------
         01110111 // result = a >> 1
    
    • & 两个相同位数的数据 同位 上的位数据 都为1,则 结果 为1,否则为0
         01110111 // a
       & 11101110 // b
    -------------
         01100110 // result = a & b
    
    • ^ 两个相同位数的数据 同位 上的位数据 只有一个1,则 结果 为1,否则为0
         11110111 // a
       ^ 01110111 // b
    -------------
         10000000 // result = a ^ b
    
    • | 两个相同位数的数据 同位 上的位数据 只要有一个1,则 结果 为1,否则为0
         11001100 // a
       | 10000101 // b
    -------------
         11001101 // result = a | b
    
    • &=, ^=, |= 与前面的功能相同,只不过多了一个对左值赋值
    bitset 操作 操作功能用法test(pos)pos 位是否为1?a.test(4)any()任意为是否为1?a.any()none()是否没有位为1?a.none()count()值是1的位的个数a.count()size()位元素的个数a.size()[pos]访问 pos 位a[4]flip()翻转所有的位a.flip()set()将所有位置1a.set()set(pos)将 pos 位置1a.set(4)reset()将所有位置0a.reset()reset(pos)将 pos 位置0a.reset(4) 自己编写一个BitSet

    提供类似以上的所有功能。

    BitSet.h
    /* BitSet.h - jave.lin 2020.05.15 为了学习用,我们就不用了,改而自己写个 BitSet */
    #pragma once
    
    #include 
    #include 
    #include 
    
    namespace BS {
    
    //
    // === statement start ===
    //
    
    #define __BS32_F(n) uint32_t f##n : 1;
    #define __BS64_F(n) uint64_t f##n : 1;
    
    struct _Bit32 {
        __BS32_F( 1)__BS32_F( 2)__BS32_F( 3)__BS32_F( 4)
        __BS32_F( 5)__BS32_F( 6)__BS32_F( 7)__BS32_F( 8)
        __BS32_F( 9)__BS32_F(10)__BS32_F(11)__BS32_F(12)
        __BS32_F(13)__BS32_F(14)__BS32_F(15)__BS32_F(16)
        __BS32_F(17)__BS32_F(18)__BS32_F(19)__BS32_F(20)
        __BS32_F(21)__BS32_F(22)__BS32_F(23)__BS32_F(24)
        __BS32_F(25)__BS32_F(26)__BS32_F(27)__BS32_F(28)
        __BS32_F(29)__BS32_F(30)__BS32_F(31)__BS32_F(32)
    };
    struct _Bit64 {
        __BS64_F( 1)__BS64_F( 2)__BS64_F( 3)__BS64_F( 4)
        __BS64_F( 5)__BS64_F( 6)__BS64_F( 7)__BS64_F( 8)
        __BS64_F( 9)__BS64_F(10)__BS64_F(11)__BS64_F(12)
        __BS64_F(13)__BS64_F(14)__BS64_F(15)__BS64_F(16)
        __BS64_F(17)__BS64_F(18)__BS64_F(19)__BS64_F(20)
        __BS64_F(21)__BS64_F(22)__BS64_F(23)__BS64_F(24)
        __BS64_F(25)__BS64_F(26)__BS64_F(27)__BS64_F(28)
        __BS64_F(29)__BS64_F(30)__BS64_F(31)__BS64_F(32)
        __BS64_F(33)__BS64_F(34)__BS64_F(35)__BS64_F(36)
        __BS64_F(37)__BS64_F(38)__BS64_F(39)__BS64_F(40)
        __BS64_F(41)__BS64_F(42)__BS64_F(43)__BS64_F(44)
        __BS64_F(45)__BS64_F(46)__BS64_F(47)__BS64_F(48)
        __BS64_F(49)__BS64_F(50)__BS64_F(51)__BS64_F(52)
        __BS64_F(53)__BS64_F(54)__BS64_F(55)__BS64_F(56)
        __BS64_F(57)__BS64_F(58)__BS64_F(59)__BS64_F(60)
        __BS64_F(61)__BS64_F(62)__BS64_F(63)__BS64_F(64)
    };
    
    using pos_t = unsigned char;
    using count_t = size_t;
    
    template
    union _UBitSet {
    public:
        using valueType = BSValueType;
    
        BSValueType value;
        BSType fs;
        _UBitSet()                                          : value((BSValueType)0) {}
        _UBitSet(const int &a)                              : value((BSValueType)a) {}
        _UBitSet(const unsigned short &a)                   : value((BSValueType)a) {}
        _UBitSet(const long &a)                             : value((BSValueType)a) {}
        _UBitSet(const long long &a)                        : value((BSValueType)a) {}
        _UBitSet(const BSValueType a)                       : value(a) {}
        _UBitSet(const _UBitSet &a)    : value(a.value) {}
        _UBitSet(const std::string &bitStr);
        _UBitSet(const char *bitChars);
        void getStr(std::string &str);
        void getStr(char *str);
        void getStrFormat(std::string &str, int interval = 8); // format只支持可变长的string,char *需要封装处理很麻烦
        // pos 位是否为1?注意pos从1开始,不是索引
        inline bool test(const pos_t pos) const;
        // 任意为是否为1?
        inline bool any() const;
        // 是否没有位为1?
        inline bool none() const;
        // 值是1的位的个数
        const count_t count() const;
        // 位元素的个数
        inline const size_t size() const;
        // 访问 pos 位
        inline BSValueType operator[](pos_t pos);
        // 翻转所有的位
        inline void flip();
        // 将所有位置1
        inline void set();
        // 将 pos 位置1
        inline void set(const pos_t pos);
        // 将所有位置0
        inline void reset();
        // 将 pos 位置0
        inline void reset(const pos_t pos);
    
        inline _UBitSet &operator=(std::string &str);
        // 其他的operator没必要重载了,因为就一个value,外部设置操作即可
    };
    
    using BitSet_32         = union _UBitSet;
    using BitSet_64         = union _UBitSet;
    
    template
    void __convertBits(T &bs, std::string &str);
    
    template
    void __getStr(T bs, std::string &str);
    
    template
    void __getStr(T bs, char *str);
    
    template
    void __getStrFormat(T bs, std::string &ret, int interval = 8);
    
    template 
    std::basic_ostream& operatorvalue == 0;
    }
    
    template
    const count_t _UBitSet::count() const {
        size_t size = sizeof(BSValueType) * 8;
        count_t count = 0;
        BSValueType v = this->value;
        for (size_t i = 0; i > i) & (1)) && ++count;
        }
        return count;
    }
    
    template
    inline const size_t _UBitSet::size() const {
        return sizeof(BSValueType) * 8;
    }
    
    template
    inline BSValueType _UBitSet::operator[](pos_t pos) {
        return (((this->value) & (((BSValueType)1) value);
    }
    
    template
    inline void _UBitSet::set() {
        this->value = -1;
    }
    
    template
    inline void _UBitSet::set(const pos_t pos) {
        this->value |= (((BSValueType)1) value &= ~(((BSValueType)1)  i) & (1)) ? '1' : '0' );
        }
    }
    
    template// T : uint32_t or uint64_t
    void __getStr(T bs, char *str) {
        size_t size = sizeof(T) * 8;
        size_t sizem1 = size - 1;
        for (size_t i = 0; i > i) & (1)) ? '1' : '0' );
        }
        str[size] = '\0';
    }
    
    template// T : uint32_t or uint64_t
    void __getStrFormat(T value, std::string &str, int interval) {
        int size = sizeof(T) * 8;
        // printf("int size = %d\n", sizeof(T));
        int addSize = size / interval;              // 需要添加多少个分隔符
        if (size % interval == 0) addSize -= 1;     // 如果分隔N份是偶数的大小就减1
        int newSize = size + addSize;               // 加上分隔符后的大小
        if (str.size()  i) & (1)) ? '1' : '0' );
            if (++idx / interval != 0) {
                idx = 0;
                ++splitterCount;
                if ((i + splitterCount)             
关注
打赏
1664331872
查看更多评论
0.0412s