1.概要
BitArray bitArray = new BitArray(9);
bitArray.Set(1, false);
bitArray.Not();
2.代码
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Collections.Immutable;
using System.Text;
namespace ConsoleApp11
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
BitArray bitArray = new BitArray(9);
bitArray.SetAll(true);
bitArray.Set(1, false);
Console.WriteLine(bitArray.GetBitsFrom());
bitArray.Not();
Console.WriteLine(bitArray.GetBitsFrom());
Console.ReadKey();
}
}
public static class BitArrayExtensions {
public static string GetBitsFrom(this BitArray bits)
{
StringBuilder sb = new StringBuilder();
for (int i = bits.Length - 1; i >= 0; i--) {
sb.Append(bits[i] ? 1 : 0);
if (i != 0 && i % 4 == 0) {
sb.Append("_");
}
}
return sb.ToString();
}
}
}
3.运行结果

这篇博客介绍了如何在C#中使用BitArray类进行位操作,包括初始化、设置位、取反等操作。示例代码展示了如何创建一个BitArray,然后将特定位置的位设置为false,接着对整个BitArray执行按位非操作,并输出结果。
1172

被折叠的 条评论
为什么被折叠?



