温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

位数组(BitArray,BitVector32)

发布时间:2020-07-04 10:40:44 来源:网络 阅读:1687 作者:1473348968 栏目:编程语言

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();

        }
    }
}

 

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI