您好,登錄后才能下訂單哦!
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對(duì)象的長(zhǎng)度要相同 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ù)組長(zhǎng)度:" + 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(); } } }
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。