BitVector32結(jié)構(gòu)效率高,位數(shù)不可變

BitArray效率低,位數(shù)可以變
========================================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 --- 位數(shù)組
//And,Or,Xor 兩個(gè)BitArray對象的長度要相同
BitArray ba1 = new BitArray(8);//11110000
BitArray ba2 = new BitArray(8);//00000000
ba1.SetAll(true);//設(shè)置數(shù)組類所有的值為true
ba1.Set(4, false);//設(shè)置索引位置4處為true
ba1.Set(5, false);
ba1.Set(6, false);
ba1.Set(7, false);
//ba1.And(ba2);//邏輯與(&&) 結(jié)果:00000000
//ba1.Or(ba2);//邏輯或(||) 結(jié)果:11110000
ba1.Xor(ba2);//異或(^) 結(jié)果:111110000
DisplayBit(ba1);
}
static void DisplayBit(BitArray b)
{
Console.WriteLine("數(shù)組長度:" + 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位的位(默認(rèn)為0)
int bit1 = BitVector32.CreateMask();//值:1 位數(shù):1
int bit2 = BitVector32.CreateMask(bit1);//值:2 位數(shù):2
int bit3 = BitVector32.CreateMask(bit2);//值:4 位數(shù):3
int bit4 = BitVector32.CreateMask(bit3);//值:8 位數(shù):4
int bit5 = BitVector32.CreateMask(bit4);//值:16 位數(shù):5
int bit6 = BitVector32.CreateMask(bit5);//值:32 位數(shù):6
int bit7 = BitVector32.CreateMask(bit6);//值:64 位數(shù):7
bv[bit7] = true;//設(shè)置第7位置位數(shù)為1
Console.WriteLine(bv.Data);//輸出int值
Console.WriteLine(bv);//輸出位數(shù)組
//---------------位數(shù)組片段化
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();
}
}
}創(chuàng)新互聯(lián)www.cdcxhl.cn,專業(yè)提供香港、美國云服務(wù)器,動態(tài)BGP最優(yōu)骨干路由自動選擇,持續(xù)穩(wěn)定高效的網(wǎng)絡(luò)助力業(yè)務(wù)部署。公司持有工信部辦法的idc、isp許可證, 機(jī)房獨(dú)有T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確進(jìn)行流量調(diào)度,確保服務(wù)器高可用性。佳節(jié)活動現(xiàn)已開啟,新人活動云服務(wù)器買多久送多久。
本文題目:位數(shù)組(BitArray,BitVector32)-創(chuàng)新互聯(lián)
當(dāng)前URL:http://chinadenli.net/article6/cdosog.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、Google、網(wǎng)站建設(shè)、服務(wù)器托管、網(wǎng)站維護(hù)、網(wǎng)頁設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容