(五十二)C#编程基础复习——C#点阵列(BitArray)
在C#中,点阵列类用来管理一个紧凑型的位值数组,数组中的值均为布尔类型,其中true(1)表示此位为开启,false(0)表示此位为关闭。
当你需要存储位(英文名“bit”数据存储的最小单位,也可称为比特),但事先又不知道具体位数时,就可以使用点阵列。当需要访问点阵列中的元素时,可以使用整型索引从点阵列中访问制定元素,索引从零开始。
一、点阵列类中的属性
下表列出了点阵列类的一些常用属性
二、点阵列类中的方法
下表列出了点阵列类中的一些常用的方法
示例代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace _046
{
internal class Program
{
static void Main(string[] args)
{
//创建两个大小为8的点阵列
BitArray ba1 = new BitArray(8);
BitArray ba2 = new BitArray(8);
byte[] a = { 60 };
byte[] b = { 13 };
//把值60和13存储到点阵列中
ba1 = new BitArray(a);
ba2 = new BitArray(b);
//ba1的内容
Console.WriteLine("点阵列ba1:60");
for(int i=0;i<ba1.Count;i++)
{
Console.Write("{0,-6}", ba1[i]);
}
Console.WriteLine();
//ba2的内容
Console.WriteLine("点阵列ba2:13");
for(int i=0;i<ba2.Count;i++)
{
Console.Write("{0,-6}", ba2[i]);
}
Console.WriteLine();
BitArray ba3 = new BitArray(8);
ba3 = ba1.And(ba2);
//ba3的内容
Console.WriteLine("执行按位与操作后的点阵列ba3:");
for(int i=0;i<ba3.Count;i++)
{
Console.Write("{0,-6}", ba3[i]);
}
Console.WriteLine();
ba3 = ba1.Or(ba2);
//ba3的内容
Console.WriteLine("执行按位或操作后的点阵列ba3:");
for(int i=0;i<ba3.Count;i++)
{
Console.Write("{0,-6}", ba3[i]);
}
Console.WriteLine();
Console.ReadKey();
}
}
}
分类:
C#基础
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了