编程语言
首页 > 编程语言> > c#-特殊的集合-BitArray-bitArray.Not()-简单实验

c#-特殊的集合-BitArray-bitArray.Not()-简单实验

作者:互联网

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#,System,bitArray,Collections,sb,BitArray,using
来源: https://blog.csdn.net/xie__jin__cheng/article/details/113823974