比特数组

  1 class BitArray
  2 {
  3 public:
  4     enum
  5     {
  6         bits_per_long = sizeof(unsigned long)*8,
  7     };
  8
  9     BitArray(): array_(0) { }
 10     BitArray(unsigned long nr): array_(0)
 11     {
 12         reset(nr);
 13     }
 14
 15     ~BitArray() { if (array_) delete [] array_; }
 16
 17     BitArray(const BitArray& rhs)
 18     {
 19         if (rhs.array_)
 20         {
 21             array_ = new unsigned long[rhs.nr_long_];
 22             memcpy(array_, rhs.array_, rhs.nr_long_ * sizeof(unsigned long));
 23             nr_long_ = rhs.nr_long_;
 24             nr_bits_ = rhs.nr_bits_;
 25         }
 26     }
 27
 28     BitArray& operator=(const BitArray& rhs)
 29     {
 30         if (this == &rhs)
 31         {
 32             return *this;
 33         }
 34
 35         if (array_)
 36         {
 37             delete [] array_;
 38         }
 39
 40         if (rhs.array_)
 41         {
 42             array_ = new unsigned long[rhs.nr_long_];
 43             memcpy(array_, rhs.array_, rhs.nr_long_ * sizeof(unsigned long));
 44             nr_long_ = rhs.nr_long_;
 45             nr_bits_ = rhs.nr_bits_;
 46         }
 47         return *this;
 48     }
 49
 50     bool operator[](std::size_t nr)
 51     {
 52         if (nr >= nr_bits_)
 53             return false;
 54
 55         unsigned long ret = array_[bitWord(nr)] & bitMask(nr);
 56         if (ret)
 57             return true;
 58         else
 59             return false;
 60     }
 61
 62     bool testBit(std::size_t nr)
 63     {
 64         return this->operator[](nr);
 65     }
 66
 67     void setBit(unsigned long nr)
 68     {
 69         if (nr >= nr_bits_)
 70         {
 71             return;
 72         }
 73
 74         array_[bitWord(nr)] |= bitMask(nr);
 75     }
 76
 77     void clearBit(unsigned long nr)
 78     {
 79         if (nr >= nr_bits_)
 80         {
 81             return;
 82         }
 83
 84         array_[bitWord(nr)] &= ~bitMask(nr);
 85     }
 86
 87     void reset(unsigned long nr_bits)
 88     {
 89         if (array_)
 90         {
 91             delete [] array_;
 92         }
 93
 94         unsigned long real_nr_bits = align(nr_bits);
 95         nr_long_ = getNrLong(real_nr_bits);
 96         array_ = new unsigned long[nr_long_];
 97         nr_bits_ = nr_bits;
 98         memset(array_, 0, nr_long_*sizeof(unsigned long));
 99     }
100
101 private:
102
103     unsigned long getNrLong(unsigned long nr) { return nr/bits_per_long; }
104     unsigned long align(unsigned long nr)
105     {
106         return (nr + (bits_per_long-1)) & (~(bits_per_long-1));
107     }
108
109     unsigned long bitWord(unsigned long nr)
110     {
111         return nr/bits_per_long;
112     }
113
114     unsigned long bitMask(unsigned long nr)
115     {
116         return nr%bits_per_long;
117     }
118
119     unsigned long nr_bits_;
120     unsigned long nr_long_;
121     unsigned long * array_;
122 };

使用如下,

#include <iostream>
#include <string>
#include <string.h>
#include "BitArray.h"

int main(void)
{
    BitArray a(10);
    a.setBit(1);
    cout << a[1] << endl;
    a.clearBit(1);
    cout << a[1] << endl;

    a.setBit(10);
    cout << a[10] << endl;

    return 0;
}
时间: 2024-07-30 01:41:20

比特数组的相关文章

C#字符串string和内存流MemoryStream及比特数组byte[]

原文:http://hi.baidu.com/endyli/item/7bf074945de35e1f934f41fe 定义string变量为str,内存流变量为ms,比特数组为bt 1.字符串转比特数组 (1)byte[] bt=System.Text.Encoding.Default.GetBytes("字符串"); (2)byte[] bt=Convert.FromBase64String("字符串"); 2.字符串转流 (1)MemoryStream ms=

字符串string和内存流MemoryStream及比特数组byte[]互转

定义string变量为str,内存流变量为ms,比特数组为bt 1.字符串转比特数组 (1)byte[] bt=System.Text.Encoding.Default.GetBytes("字符串"); (2)byte[] bt=Convert.FromBase64String("字符串"); 2.字符串转流 (1)MemoryStream ms=new MemoryStream(System.Text.Encoding.Default.GetBytes("

C#字符串、字节数组和内存流间的相互转换 - IT浪潮之巅

定义string变量为str,内存流变量为ms,比特数组为bt 1.字符串=>比特数组 (1)byte[] bt=System.Text.Encoding.Default.GetBytes("字符串"); (2)byte[] bt=Convert.FromBase64String("字符串"); 补充: System.Text.Encoding.Unicode.GetBytes(str); System.Text.Encoding.UTF8.GetBytes(

C# 字符串和字节数组转换

转自:http://blog.sina.com.cn/s/blog_683d60ff0100rhwk.html 定义string变量为str,内存流变量为ms,比特数组为bt 1.字符串转比特数组 (1)byte[] bt=System.Text.Encoding.Default.GetBytes("字符串"); (2)byte[] bt=Convert.FromBase64String("字符串"); 2.字符串转流 (1)MemoryStream ms=new

C#中字符数组,字节数组和string之间的转化(转)

原文链接:http://hi.baidu.com/endyli/item/7bf074945de35e1f934f41fe 来源: NDC(NetworkDiskClient)的界面和后台程序之间用Socket通信,发送命令. 环境:界面:C# winform 后台:Vc++,消息通知 网络通信,C#是通过网络字节流进行传输的,传输内容是有报文头的Protobuf.Net消息.报文头是struct结构体,先转化成 byte[],protobuf消息就先转换为内存流,再stream.ToArray

C#字符串、字节数组和内存流间的相互转换

定义string变量为str,内存流变量为ms,比特数组为bt 1.字符串=>比特数组 (1)byte[] bt=System.Text.Encoding.Default.GetBytes("字符串"); (2)byte[] bt=Convert.FromBase64String("字符串"); 补充: System.Text.Encoding.Unicode.GetBytes(str); System.Text.Encoding.UTF8.GetBytes(

数组与链表的应用-位图数组在Redis中的应用

位数组(Bit Array),这种数组如何在Redis中应用? 统计每个月学习专栏的用户活跃度 在开始之前,先来考虑一个关于用户行为分析的问题,假设要统计<数据结构精讲:从原理到实战>这个专栏每个月的用户活跃度.在每个月中只要有用户登录并且学习这个专栏,都会将这个用户的ID写入一张MySql表中,如果想知道2019年11和12这两个月内都在学习这个专栏的用户有多少,应该怎么做呢? 很直观的做法是执行类型下面的sql语句 1 SELECT COUNT(*) FROM nov_stats 2 IN

Webform(文件上传)

1.HTML编码: <input type="file" /> 2.控件:FileUpload 它是用来选择要上传的文件,还需要一个按钮来将选中的文件上传到服务器上 string path = "images/" + FileUpload1.FileName;FileUpload1.SaveAs(Server.MapPath(path)); 优化1:文件保留原有名称和后缀 string path = "images/" + FileU

.net网站的文件上传读取进度条和断点下载

文件上传到服务器时的进度读取 UpfileResult result = new UpfileResult(); try { //先把文件预读到内存里,同时计算上传进度 IServiceProvider provider = (IServiceProvider)HttpContext.Current; // 返回 HTTP 请求正文已被读取的部分. HttpWorkerRequest request = (HttpWorkerRequest)provider.GetService(typeof(