位数组(BitArray,BitVector32)-创新互联
BitVector32结构效率高,位数不可变

BitArray效率低,位数可以变
========================================BitArray
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
//BitArray --- 位数组
//And,Or,Xor 两个BitArray对象的长度要相同
BitArray ba1 = new BitArray(8);//11110000
BitArray ba2 = new BitArray(8);//00000000
ba1.SetAll(true);//设置数组类所有的值为true
ba1.Set(4, false);//设置索引位置4处为true
ba1.Set(5, false);
ba1.Set(6, false);
ba1.Set(7, false);
//ba1.And(ba2);//逻辑与(&&) 结果:00000000
//ba1.Or(ba2);//逻辑或(||) 结果:11110000
ba1.Xor(ba2);//异或(^) 结果:111110000
DisplayBit(ba1);
}
static void DisplayBit(BitArray b)
{
Console.WriteLine("数组长度:" + b.Count);
foreach (bool item in b)
{
Console.Write(item ? 1 : 0);
}
Console.ReadKey();
}
}
}========================================BitVector32
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Specialized;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
//----------------使用掩码和索引器访问矢量中的位
BitVector32 bv = new BitVector32();//初始化32位的位(默认为0)
int bit1 = BitVector32.CreateMask();//值:1 位数:1
int bit2 = BitVector32.CreateMask(bit1);//值:2 位数:2
int bit3 = BitVector32.CreateMask(bit2);//值:4 位数:3
int bit4 = BitVector32.CreateMask(bit3);//值:8 位数:4
int bit5 = BitVector32.CreateMask(bit4);//值:16 位数:5
int bit6 = BitVector32.CreateMask(bit5);//值:32 位数:6
int bit7 = BitVector32.CreateMask(bit6);//值:64 位数:7
bv[bit7] = true;//设置第7位置位数为1
Console.WriteLine(bv.Data);//输出int值
Console.WriteLine(bv);//输出位数组
//---------------位数组片段化
BitVector32 bv2 = new BitVector32(0x79abcdef);
BitVector32.Section section1 = BitVector32.CreateSection(0xfff);//12位
BitVector32.Section section2 = BitVector32.CreateSection(0xff, section1);//接着 8位
BitVector32.Section section3 = BitVector32.CreateSection(0xff, section2);//接着 8位
BitVector32.Section section4 = BitVector32.CreateSection(0xf, section3);//接着 4位
Console.WriteLine(bv2);//0111 10011010 10111100 110111101111
Console.WriteLine(Convert.ToString(bv2[section1], 2));//110111101111
Console.WriteLine(Convert.ToString(bv2[section2], 2));//10111100
Console.WriteLine(Convert.ToString(bv2[section3], 2));//10011010
Console.WriteLine(Convert.ToString(bv2[section4], 2));//111
Console.ReadKey();
}
}
}创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。
本文标题:位数组(BitArray,BitVector32)-创新互联
网页链接:http://www.jxjierui.cn/article/cdosog.html


咨询
建站咨询
